Determine which JSON was received.

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
mfulmer72
Posts: 2
Joined: Fri Oct 27, 2023 4:14 pm

Determine which JSON was received.

Post by mfulmer72 »

I am working with a Client to implement the new UPS API's that are changing from XML to JSON. Currently I'm testing the Address Validation API. I am receiving the JSON File to the IFS and using YAJL in Free Form RPGLE to parse. From the Address Validation Request, I can receiving 2 different Address Validation Response Files. I need to determine programmatically which JSON File was received so I can execute the correct parsing routine. I used YAJLGEN to create 2 different Data Structures and YAJLINTO for parsing. I know File #2 has an array of Addresses, but I haven't found a way to recognize the existence or non existence of the array in the received files. What would be the best way to determine which file was received?

File #1:

{
"XAVResponse": {
"Response": {
"ResponseStatus": {
"Code": "1",
"Description": "Success"
}
},
"ValidAddressIndicator": "",
"AddressClassification": {
"Code": "2",
"Description": "Residential"
},
"Candidate": {
"AddressClassification": {
"Code": "2",
"Description": "Residential"
},
"AddressKeyFormat": {
"AddressLine": "54 CLAUDETTE CT",
"PoliticalDivision2": "BUFFALO",
"PoliticalDivision1": "NY",
"PostcodePrimaryLow": "14225",
"PostcodeExtendedLow": "3802",
"Region": "BUFFALO NY 14225-3802",
"CountryCode": "US"
}
}
}
}

File #2:

{
"XAVResponse": {
"Response": {
"ResponseStatus": {
"Code": "1",
"Description": "Success"
}
},
"AmbiguousAddressIndicator": "",
"AddressClassification": {
"Code": "0",
"Description": "Unknown"
},
"Candidate": [
{
"AddressClassification": {
"Code": "0",
"Description": "Unknown"
},
"AddressKeyFormat": {
"AddressLine": "5 EAST AVE N",
"PoliticalDivision2": "FALCONER",
"PoliticalDivision1": "NY",
"PostcodePrimaryLow": "14733",
"PostcodeExtendedLow": "1301",
"Region": "FALCONER NY 14733-1301",
"CountryCode": "US"
}
},
{
"AddressClassification": {
"Code": "0",
"Description": "Unknown"
},
"AddressKeyFormat": {
"AddressLine": "5 EAST AVE S",
"PoliticalDivision2": "FALCONER",
"PoliticalDivision1": "NY",
"PostcodePrimaryLow": "14733",
"PostcodeExtendedLow": "1303",
"Region": "FALCONER NY 14733-1303",
"CountryCode": "US"
}
},
{
"AddressClassification": {
"Code": "0",
"Description": "Unknown"
},
"AddressKeyFormat": {
"AddressLine": "5 EAST AVE S",
"PoliticalDivision2": "FALCONER",
"PoliticalDivision1": "NY",
"PostcodePrimaryLow": "14733",
"PostcodeExtendedLow": "1304",
"Region": "FALCONER NY 14733-1304",
"CountryCode": "US"
}
},
{
"AddressClassification": {
"Code": "0",
"Description": "Unknown"
},
"AddressKeyFormat": {
"AddressLine": "5 EAST AVE N",
"PoliticalDivision2": "FALCONER",
"PoliticalDivision1": "NY",
"PostcodePrimaryLow": "14733",
"PostcodeExtendedLow": "1302",
"Region": "FALCONER NY 14733-1302",
"CountryCode": "US"
}
}
]
}
}
peder udesen
Posts: 15
Joined: Thu Jul 29, 2021 8:00 am

Re: Determine which JSON was received.

Post by peder udesen »

A quick and dirty solution could be to open the file and read the first 1MB into a variable.
Then scan for the string "AmbiguousAddressIndicator":

If it is found then it is file 2 else it is file 1

Don't forget to close the file after reading :)
mfulmer72
Posts: 2
Joined: Fri Oct 27, 2023 4:14 pm

Re: Determine which JSON was received.

Post by mfulmer72 »

All is working now. After a few days away from the keyboard, I realized that if I added allowmissing=yes allowextra=yes to the data-into parameters, then the Data Structure created by YAJLGEN was correct and all I needed to do was handle the arrays.
Post Reply