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

Modify existing JSON

Post by brianjgarland »

I have a task where I need to read a database file that has a column that contains a JSON string. I will then execute code based on the value of a key in that string and then modify the value for that key and update the table. All that is pretty straightforward except for "modify the value for that key". I've looked through the service programs but nothing jumps out at me. Is there a procedure that would allow me to change the value for a key?

Example input string:

Code: Select all

{"name1":"value1","name2":"value2","flag":1}
Task, change the value of "flag" to 2 so it results in this:

Code: Select all

{"name1":"value1","name2":"value2","flag":2}
I know I could just replace the value using %SCAN and %SUBST but I'd prefer to do it through yajl procedures if possible.

Thanks,
Brian
jonboy49
Posts: 200
Joined: Wed Jul 28, 2021 8:18 pm

Re: Modify existing JSON

Post by jonboy49 »

I have never come across any YAJL routines to do this.

When I needed to do the same thing a while back I used DATA-INTO with YAJLINTO to parse the JSON into a DS. Made the changes. Then used DATA-GEN with YAJLGEN to recreate the JSON.

Would that work for your scenario?
brianjgarland
Posts: 14
Joined: Wed Jul 28, 2021 11:04 am
Location: Vermont, USA
Contact:

Re: Modify existing JSON

Post by brianjgarland »

I don't have the full definition of the JSON so DATA-INTO is a problem.

I'm sure I can get the definition so that might be worth a try.

Thank you for the suggestion.
Scott Klement
Site Admin
Posts: 636
Joined: Sun Jul 04, 2021 5:12 am

Re: Modify existing JSON

Post by Scott Klement »

YAJL doesn't currently have that ability. You'd need to load everything into your program, then write it back as a new document with a different value.

As Jon points out, that'd be very easy to do with DATA-INTO/DATA-GEN.
Post Reply