1. Igor Perisic
  2. PowerBuilder
  3. Tuesday, 17 January 2023 20:22 PM UTC

Hi,

I am working on a few different things at the moment, JSON being one of them. I was wondering how can I edit my JSON file that has nested objects (i think that's the correct the term). In other words, a JSON structure that looks like this.

{
    "Application":
    {
        "trace": "yes",
        "timeout": "4",
        "appName": "test"
    },

    "Database":
    {
        "username": "UserTest",
        "password": "1234",
        "server": "myserver"
    }
}

My goal is to change the "timeout" value that is inside the "application" object for example. I am currently using this code

jsonString = create JsonPackage
checkError = jsonString.LoadFile(Appl + ".json")

if Len(checkError) = 0 then
	result = jsonString.SetValue("timeout","12", false)
	if result = 1 then
		jsonString.SaveToFile(Appl + ".json", EncodingANSI!)
	end if
end if

But this doesn't edit the "timeout" value inside the "application" object, instead it just creates a new object after "Database". I figure out how to read nested objects using, "GetRootItem(), "GetItemObject(), GetItemString()"  but I cannot figure out how to set values the values. There doesn't seem to be a complimentary "SetItemString()" function to those Get functions. I can't find anything in the documentation. 

Thanks!

Accepted Answer
Benjamin Gaesslein Accepted Answer Pending Moderation
  1. Wednesday, 18 January 2023 07:16 AM UTC
  2. PowerBuilder
  3. # Permalink

Unfortunately this isn't as simple as it should be. The only way I've found to work is to create a new jsonPackage object for the nested object. I use loadstring to do that. Then set the value there and use that new jsonPackage object to replace the object in the original jsonPackage object. Something like this should work:

jsonPackage root
jsonPackage nested

root = create jsonPackage
root.loadfile( App + ".json" )

nested = create jsonPackage
nested.loadstring( root.getvalue("Application") )
nested.setvalue( "timeout", "12", false )
root.setvalue( "Application", nested.getjsonstring() )

 

Comment
  1. John Strano
  2. Wednesday, 18 January 2023 12:59 PM UTC
This was very useful...and talk about timely!



I came across this need myself just yesterday.



The universe is a strange thing.
  1. Helpful
  1. Igor Perisic
  2. Wednesday, 18 January 2023 18:03 PM UTC
So far that is working great for me, thanks Benjamin!!
  1. Helpful
  1. Benjamin Gaesslein
  2. Thursday, 19 January 2023 07:17 AM UTC
Glad I could help you both.

Of course this is very barebones and some code should probably be added to check if the keys exist etc. but this should do in a pinch. To make it more re-usable a recursive function or a loop can be used to set values that are more deeply nested.
  1. Helpful 2
There are no comments made yet.
Sivaprakash BKR Accepted Answer Pending Moderation
  1. Wednesday, 18 January 2023 08:00 AM UTC
  2. PowerBuilder
  3. # 1

Hello,

Here is the PB 2019 R3 project that does the job.   Just run the application after unzipping.  It will give you the result.

Here I've used u_json object that's available in codeXchange.

HTH

Happiness Always
BKR Sivaprakash

 

Attachments (1)
Comment
  1. Sivaprakash BKR
  2. Wednesday, 18 January 2023 08:02 AM UTC
I've modified the password data available in the 2nd node. Hope the code is easy to understand that you can modify any value, by modifying the corresponding index value.
  1. Helpful
  1. Igor Perisic
  2. Wednesday, 18 January 2023 18:03 PM UTC
Thanks for the help :)
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.