Page 1 of 1
DATA-INTO when entire JSON Document is an array
Posted: Wed Mar 08, 2023 9:00 pm
by kdanforth
I get the following response from an API call. I want to use DATA-INTO to parse it. I've used DATA-INTO with plenty of JSON responses in the past, but this is the first time when the entire document is an array. What would the data structure look like?
On a side note, I can't use "Rel" as a DS subfield as this is apparently a reserved word. I don't actually need the field so I've removed it from the DS and specified "allowmissing=yes" in the data options, but it would be good to know how to handle this in the future.
API Response:
[
{
"Rel": "detail",
"Href": "
https://apisandbox.electioapp.com/consi ... 00-0C8-FJX"
}
]
Re: DATA-INTO when entire JSON Document is an array
Posted: Wed Mar 08, 2023 10:21 pm
by jonboy49
Just code it as a DS array like so:
Code: Select all
**free
dcl-s input varchar(200)
inz( '[{"Rel": "detail","Href": "https://apisandbox.electioapp.com"}]' );
dcl-ds results dim(99) qualified;
// rel char(20);
href varchar(200);
end-ds;
data-into results %data( input: 'case=any allowextra=yes')
%parser('YAJL/YAJLINTO');
The reason that "rel" is not accepted as a field name is that it is the name of an opcode (RELease) - luckily you don't need it!
And ... you need allowextra=yes - not missing as there is data in the document (rel) with no home!
Re: DATA-INTO when entire JSON Document is an array
Posted: Wed Mar 08, 2023 10:30 pm
by brianjgarland
If in the future you need rel, you can just define it using dcl-subf inside your DS.
Re: DATA-INTO when entire JSON Document is an array
Posted: Thu Mar 09, 2023 1:07 am
by jonboy49
Duh ... I knew there was a way of doing that but had forgotten. Thanks for the memory jog Brian.
Re: DATA-INTO when entire JSON Document is an array
Posted: Thu Mar 09, 2023 5:44 am
by Scott Klement
If you want to include "rel", you can... you just have to put "dcl-subf" in there so it knows it's not the opcode.
Code: Select all
dcl-ds results dim(99) qualified;
dcl-subf rel char(20);
href varchar(200);
end-ds;
Re: DATA-INTO when entire JSON Document is an array
Posted: Fri Mar 10, 2023 4:14 am
by kdanforth
Thank you all. Exactly what I needed.