DATA-INTO when entire JSON Document is an array

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
kdanforth
Posts: 3
Joined: Fri Jun 24, 2022 8:09 am

DATA-INTO when entire JSON Document is an array

Post 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"
}
]
jonboy49
Posts: 200
Joined: Wed Jul 28, 2021 8:18 pm

Re: DATA-INTO when entire JSON Document is an array

Post 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!
brianjgarland
Posts: 14
Joined: Wed Jul 28, 2021 11:04 am
Location: Vermont, USA
Contact:

Re: DATA-INTO when entire JSON Document is an array

Post by brianjgarland »

If in the future you need rel, you can just define it using dcl-subf inside your DS.
jonboy49
Posts: 200
Joined: Wed Jul 28, 2021 8:18 pm

Re: DATA-INTO when entire JSON Document is an array

Post by jonboy49 »

Duh ... I knew there was a way of doing that but had forgotten. Thanks for the memory jog Brian.
Scott Klement
Site Admin
Posts: 636
Joined: Sun Jul 04, 2021 5:12 am

Re: DATA-INTO when entire JSON Document is an array

Post 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;
kdanforth
Posts: 3
Joined: Fri Jun 24, 2022 8:09 am

Re: DATA-INTO when entire JSON Document is an array

Post by kdanforth »

Thank you all. Exactly what I needed.
Post Reply