1. Mark Winsor
  2. PowerBuilder
  3. Friday, 3 March 2023 21:19 PM UTC

I cannot seem to generate an array of named objects using JSONGenerator.

If I use the following code:

Jsongenerator jga
Long ll_child
Long ll_jsonarray
long ll_handle
long ll_root
String ls_json1

jga = create JSONGenerator
ll_root = jga.CreateJsonObject()
ll_jsonarray = jga.AddItemArray(ll_root, "packages")
ll_child = jga.AddItemobject(ll_jsonarray)
ll_handle = jga.AddItemNumber(ll_child,"value", 4.5)
ll_handle =jga.AddItemString(ll_child,"unit", "pound")
ll_child = jga.AddItemobject(ll_jsonarray)
ll_handle = jga.AddItemNumber(ll_child,"value", 6.8)
ll_handle =jga.AddItemString(ll_child,"unit", "pound")
ls_json1 = jga.getjsonstring( )

 

I get these results:

{
"packages":
[
{"value":4.5,"unit":"pound"},
{"value":6.8,"unit":"pound"}
]
}

Which is close to what I need but what I really want is:

{
"packages":
[
{ "weight": {"value":4.5,"unit":"pound"} },
{ "weight": {"value":6.8,"unit":"pound"} }
]
}

So it seemed logical to change my AddItemObject statement from 

ll_child = jga.AddItemobject(ll_jsonarray) 

to this:

ll_child = jga.AddItemobject(ll_jsonarray, "weight")

but when I do that I get:

{"packages":[]}

 

Is there a way to get the generator to create more complex json or is it just a bit too primitive a tool to get the output I need? 

 

Arnd Schmidt Accepted Answer Pending Moderation
  1. Saturday, 4 March 2023 12:51 PM UTC
  2. PowerBuilder
  3. # 1
Jsongenerator jga
Long ll_child
Long ll_jsonarray
long ll_handle
long ll_root
String ls_json1

jga = create JSONGenerator
ll_root = jga.CreateJsonObject()
ll_jsonarray = jga.AddItemArray(ll_root, "packages")
ll_child = jga.AddItemobject(ll_jsonarray)
ll_child = jga.AddItemobject(ll_child,"weight")
ll_handle = jga.AddItemNumber(ll_child,"value", 4.5)
ll_handle =jga.AddItemString(ll_child,"unit", "pound")
ll_child = jga.AddItemobject(ll_jsonarray)
ll_child = jga.AddItemobject(ll_child,"weight")
ll_handle = jga.AddItemNumber(ll_child,"value", 6.8)
ll_handle =jga.AddItemString(ll_child,"unit", "pound")
ls_json1 = jga.getjsonstring( )

hth

Arnd

Comment
  1. Mark Winsor
  2. Saturday, 4 March 2023 15:35 PM UTC
That did it. I'm not sure why I need to add an object between the array and the "weight" object but it did the trick.
  1. Helpful
There are no comments made yet.
Sivaprakash BKR Accepted Answer Pending Moderation
  1. Saturday, 4 March 2023 07:32 AM UTC
  2. PowerBuilder
  3. # 2

{
"packages":
[
{ "weight": {"value":4.5,"unit":"pound"} },
{ "weight": {"value":6.8,"unit":"pound"} }
]
}

The above json is very much doable in PB.   To add value and unit, you need to create another object and add that to weight.  

Since I'm using u_json utility [ available in CodeXchange ], I'm more comfortable generating json with that.   

HTH

Happiness Always
BKR Sivaprakash

 

Comment
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.