JSON Arrays Not Consistent

This forum allows users to post and respond to "How Do I Do ....." questions. The information contained in this forum has not been validated by K-Rise Systems and, as such, K-Rise Systems cannot guarantee the accuracy of the information.
Post Reply
JefferyD
Posts: 177
Joined: August 31st, 2021, 11:37 am
Contact:

JSON Arrays Not Consistent

Unread post 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": ""
				}
			]
		}
	}
}
Last edited by JefferyD on March 15th, 2022, 5:33 pm, edited 1 time in total. word count: 82

Tags:
SteveCap
Posts: 327
Joined: August 26th, 2021, 9:18 am
Contact:

Re: JSON Arrays Not Consistent

Unread post 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>
word count: 42
JefferyD
Posts: 177
Joined: August 31st, 2021, 11:37 am
Contact:

Re: JSON Arrays Not Consistent

Unread post 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 649 times
API JSON Error.PNG
API JSON Error.PNG (3.71 KiB) Viewed 649 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 649 times
API Array Logs.PNG
API Array Logs.PNG (60.62 KiB) Viewed 649 times
word count: 83
SteveCap
Posts: 327
Joined: August 26th, 2021, 9:18 am
Contact:

Re: JSON Arrays Not Consistent

Unread post 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>
word count: 65
JefferyD
Posts: 177
Joined: August 31st, 2021, 11:37 am
Contact:

Re: JSON Arrays Not Consistent

Unread post by JefferyD »

Thanks, that worked exactly as I needed it to.
word count: 9
Post Reply