Page 1 of 1

JSON Arrays Not Consistent

Posted: February 24th, 2022, 9:45 am
by JefferyD
I'm working on making REST APIs, but I'm having a little difficulty making the response consistent.

This is how the response returns normally.

Code: Select all

{
	"Response": {
		"Employees": {
			"Employee": [
				{
					"ContractorFormID": "1114",
					"ContractorAddressBookNum": ""
				},
				{
					"ContractorFormID": "1116",
					"ContractorAddressBookNum": ""
				}
			]
		}
	}
}

When only a single employee is returned though, it's no longer in an array.

Code: Select all

{
	"Response": {
		"Employees": {
			"Employee": {
				"ContractorFormID": "1114",
				"ContractorAddressBookNum": ""
			}
		}
	}
}

Is there any way to force it be an array like this?

Code: Select all

{
	"Response": {
		"Employees": {
			"Employee": [
				{
					"ContractorFormID": "1114",
					"ContractorAddressBookNum": ""
				}
			]
		}
	}
}

Re: JSON Arrays Not Consistent

Posted: February 24th, 2022, 10:45 am
by SteveCap
Is this created as xml?

If so you can try adding in an attribute.

Code: Select all

<Employees>
	<Employee json:Array="True">
		<ContractorFormID></ContractorFormID>
		<ContractorAddressBookNum></ContractorAddressBookNum>
	</Employee>
</Employees>
Or

Code: Select all

<Employees>
	<Employee jsonArray="True">
		<ContractorFormID></ContractorFormID>
		<ContractorAddressBookNum></ContractorAddressBookNum>
	</Employee>
</Employees>

Re: JSON Arrays Not Consistent

Posted: February 24th, 2022, 11:21 am
by JefferyD
Trying both, I'm getting this error in the API tester. I also tried on the "Employees" node, but it was the same result there too.
API JSON Error.PNG
API JSON Error.PNG (3.71 KiB) Viewed 1279 times
API JSON Error.PNG
API JSON Error.PNG (3.71 KiB) Viewed 1279 times

The logs don't show any errors though, so I'm not sure where the problem is.
API Array Logs.PNG
API Array Logs.PNG (60.62 KiB) Viewed 1279 times
API Array Logs.PNG
API Array Logs.PNG (60.62 KiB) Viewed 1279 times

Re: JSON Arrays Not Consistent

Posted: February 24th, 2022, 12:00 pm
by SteveCap
I did some test on my own. You need to also add in the namespace so either of these should work.

Code: Select all

<Employees>
	<Employee json:Array="true" xmlns:json="http://james.newtonking.com/projects/json">
		<ContractorFormID></ContractorFormID>
		<ContractorAddressBookNum></ContractorAddressBookNum>
	</Employee>
</Employees>
OR

Code: Select all

<Employees>
	<Employee jsonArray="true" xmlns:json="http://james.newtonking.com/projects/json">
		<ContractorFormID></ContractorFormID>
		<ContractorAddressBookNum></ContractorAddressBookNum>
	</Employee>
</Employees>

Re: JSON Arrays Not Consistent

Posted: February 24th, 2022, 3:53 pm
by JefferyD
Thanks, that worked exactly as I needed it to.