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"
}
]
DATA-INTO when entire JSON Document is an array
Re: DATA-INTO when entire JSON Document is an array
Just code it as a DS array like so:
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!
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');
And ... you need allowextra=yes - not missing as there is data in the document (rel) with no home!
-
- Posts: 17
- Joined: Wed Jul 28, 2021 11:04 am
- Location: Vermont, USA
- Contact:
Re: DATA-INTO when entire JSON Document is an array
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
Duh ... I knew there was a way of doing that but had forgotten. Thanks for the memory jog Brian.
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: DATA-INTO when entire JSON Document is an array
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
Thank you all. Exactly what I needed.