1. Andrew Davis
  2. PowerBuilder
  3. Monday, 11 June 2018

Hi, I am using the JsonGenerator and I am having trouble getting it formatted correctly with my arrays - square brackets are missing

This is what I am trying to create

{
    "userId": "adsf1234",
    "dueDate": "2018/02/21",
    "items": [
        {
            "sku": "shirt",
            "id": 234567,
            "extras": [
                {
                    "type": "silk",
                    "price": 2.00,
                }
            ],
            "notes": "",
        },
        {
            "sku": "shirt",
            "id": 234568,
            "notes": "",
        },
        {
    ]
}

********* this is my code - 

ll_childobect = lnv_JsonGenerator.CreateJsonObject()

lnv_JsonGenerator.AddItemString(ll_ChildObject, "userId", 'W2DBZqIkce')
lnv_JsonGenerator.AddItemString(ll_ChildObject, "dueDate", "14/06/2018")

ll_item_array=lnv_JsonGenerator.additemobject(ll_childobject,"items")

lnv_JsonGenerator.AddItemString(ll_item_array, "sku", "shirt")
lnv_JsonGenerator.AddItemString(ll_item_array, "description", 'white shirt')

********* this is what I get

{
"userId":"W2DBZqIkce",
"dueDate":"14/06/2018",
"items":
      {
      "sku":"shirt",
      "description":"white_shirt"
      }
}
 
***** the square brackets are missing
 
 
** also is there any more documentation or example of using the generator 

 

Alfredo Aldama Accepted Answer Pending Moderation
0
Votes
Undo
Hello, Check how many levels nesting are possible with PB. Regards!
Comment
do you know how many levels are possible ?

  1. Andrew Davis
  2. Wednesday, 13 June 2018
There are no comments made yet.
  1. Tuesday, 12 June 2018
  2. PowerBuilder
  3. # 1
Govinda Lopez Accepted Answer Pending Moderation
0
Votes
Undo

Hi Andrew,

 

Please try the AddItemArray function. 

 

This example creates an object root item and adds an array child item. The result is {"id":[101,102,103]}.

JsonGenerator lnv_JsonGenerator
lnv_JsonGenerator = Create JsonGenerator

// Create an object root item
Long ll_RootObject = lnv_JsonGenerator.CreateJsonObject()

// Add an array child item
Long ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootObject, "id")
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 101)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 102)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 103)

You may find more about this here (page 305). Or you can also download an example app that uses the the JSonGenerator object here.

 

 

Regards,

Comment
I tried this previously - and i can add this however in my requirement the array needs to have 



in your example "id": [ "key":"string", "key2","string2"] .....



if i try to add object it just doesnt add



 

  1. Andrew Davis
  2. Wednesday, 13 June 2018
There are no comments made yet.
  1. Monday, 11 June 2018
  2. PowerBuilder
  3. # 2
Simone Olianti Accepted Answer Pending Moderation
0
Votes
Undo

try this:

ll_item_array=lnv_JsonGenerator.additemarray(ll_childobject,"items")

Comment
There are no comments made yet.
  1. Monday, 11 June 2018
  2. PowerBuilder
  3. # 3
Kevin Ridley Accepted Answer Pending Moderation
0
Votes
Undo

I haven't used the JSONGenerator yet, but a quick look at the object shows a function - AddItemArray.  I'd take a look at that function, it should do what you need.

Comment
There are no comments made yet.
  1. Monday, 11 June 2018
  2. PowerBuilder
  3. # 4
  • Page :
  • 1


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