Page 1 of 1

Modify existing JSON

Posted: Thu Feb 24, 2022 4:15 pm
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

Re: Modify existing JSON

Posted: Thu Feb 24, 2022 6:54 pm
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?

Re: Modify existing JSON

Posted: Thu Feb 24, 2022 7:37 pm
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.

Re: Modify existing JSON

Posted: Fri Feb 25, 2022 4:25 am
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.