Consume Dynamic JSON

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
kathan.p@gmail.com
Posts: 19
Joined: Wed Aug 04, 2021 7:27 pm

Consume Dynamic JSON

Post by kathan.p@gmail.com »

I have an old application in which it is receiving multiple JSON with data elements in it . The only common element in all those JSON is a field name "title" which will give some heading and based on that heading we write different subroutine/procedure to extract the data and as it dynamic and fields are different in every JSON i have to hard code the key and then retrieve it's value. I was trying to make it more simpler by using two ways:-

1) Using DATA-INTO and create DS for every JSON and get the data but the issue is some JSON have common field name so i can't declare same field name in multiple DS. and if i change the field name not sure DATA-INTO will work or not? But still i have to declare multiple DS.

2) write a program to get the "KEY" name from JSON and then store it in an Array / temp file and then read that Array/Temp file and get all the values using

Code: Select all

YAJL_OBJECT_FIND(<arrayname[index]/temp file field name)


In option 2) i can reduce my code by the putting above line in loop and reading it into the loop and storing the data whenever i want..
But i don't know if there is any method to get only KEY names in JSON? other then find it using %SCAN, comma, double quotes ...etc..etc

JSON EXAMPLE:-

Code: Select all

{"data":{"odom":2023,"tRead":"311","teMde":"PED"
},title-"JSON1"}

{"data":{"om":"208706.05","fix1":"36","lastread":"PRO"
},title-"JSON2"}

Any suggestion to handle such cases\?
jonboy49
Posts: 207
Joined: Wed Jul 28, 2021 8:18 pm

Re: Consume Dynamic JSON

Post by jonboy49 »

Your option 1 includes an incorrect assumption. You CAN have the same name in multiple DS you just have to specify them as qualified. In fact that is exactly the way I have handled this scenario in the past.
Scott Klement
Site Admin
Posts: 667
Joined: Sun Jul 04, 2021 5:12 am

Re: Consume Dynamic JSON

Post by Scott Klement »

kathan.p@gmail.com wrote: Wed Aug 11, 2021 5:54 pm 2) write a program to get the "KEY" name from JSON and then store it in an Array / temp file and then read that Array/Temp file and get all the values using YAJL_OBJECT_FIND(<arrayname[index]/temp file field name)
I don't understand what the purpose of the array or temp file is? Why not just read all of the data and use it? What's the purpose of storing the keys somewhere and then re-reading and getting the data from the keys?
kathan.p@gmail.com wrote: Wed Aug 11, 2021 5:54 pmIn option 2) i can reduce my code by the putting above line in loop and reading it into the loop and storing the data whenever i want.. But i don't know if there is any method to get only KEY names in JSON? other then find it using %SCAN, comma, double quotes ...etc..etc
You could use YAJL_OBJECT_LOOP or YAJL_OBJECT_ELEM/YAJL_OBJECT_SIZE to get all of the data in the JSON objects (this gives both the key and the value) and then just don't use the values for anything, just the keys.

But... it'd be a LOT more efficient to just use the values while looping through YAJL_OBJECT_LOOP or YAJL_OBJECT_ELEM/YAJL_OBJECT_SIZE than it is to save all the keys, then make a second pass and cal YAJL_OBJECT_FIND for each key. That seems like a very strange thing to do. I guess it makes sense if you wanted to do something like give the user a list of keys, and then only select the ones the user asked about.
kathan.p@gmail.com wrote: Wed Aug 11, 2021 5:54 pm JSON EXAMPLE:-

Code: Select all

{"data":{"odom":2023,"tRead":"311","teMde":"PED"
},title-"JSON1"}

{"data":{"om":"208706.05","fix1":"36","lastread":"PRO"
},title-"JSON2"}
This isn't a valid JSON document. You can't separate the key name from its value with a dash.

You have this:

Code: Select all

title-"JSON1"
They must be separated by a colon character like this, instead:

Code: Select all

title:"JSON1"
And you can't put two individual objects in the same document like that. Either you need to make these two separate documents, or you need to put them into an array, or create an object that has both of your objects as sub-elements.
Post Reply