1. Andrew Davis
  2. PowerBuilder
  3. Monday, 11 June 2018 15:11 PM UTC

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 

 

Ken Guo @Appeon Accepted Answer Pending Moderation
  1. Monday, 18 January 2021 11:51 AM UTC
  2. PowerBuilder
  3. # 1

Hi Andrew,

Please run the following code, and then you can generate the Json result you want:

long  ll_ChildObject,ll_Item_Array,ll_Item_Object, ll_Subitem_Object,ll_Subitem_Array
JsonGenerator lnv_JsonGenerator
lnv_JsonGenerator = Create JsonGenerator
ll_ChildObject = lnv_JsonGenerator.CreateJsonObject()

lnv_JsonGenerator.AddItemString(ll_ChildObject, "userId", 'adsf1234')
lnv_JsonGenerator.AddItemString(ll_ChildObject, "dueDate", "2018-02-21")

//Array 1
ll_Item_Object = lnv_JsonGenerator.AddItemArray(ll_Childobject,"items")
ll_Item_Array=lnv_JsonGenerator.AddItemObject(ll_Item_Object)
lnv_JsonGenerator.AddItemString(ll_Item_Array, "sku", "shirt")
lnv_JsonGenerator.AddItemString(ll_Item_Array, "id", 'w234567')
ll_Subitem_Object = lnv_JsonGenerator.AddItemArray(ll_Item_Array,"extras")
ll_Subitem_Array=lnv_JsonGenerator.AddItemObject(ll_Subitem_Object)
lnv_JsonGenerator.AddItemString(ll_Subitem_Array, "type", "silk")
lnv_JsonGenerator.AddItemString(ll_Subitem_Array, "price", '2.00')
lnv_JsonGenerator.AddItemString(ll_Item_Array, "notes", '')

//Array 2
ll_Item_Array=lnv_JsonGenerator.AddItemObject(ll_Item_Object)
lnv_JsonGenerator.AddItemString(ll_Item_Array, "sku", "shirt")
lnv_JsonGenerator.AddItemString(ll_Item_Array, "id", '234568')
lnv_JsonGenerator.AddItemString(ll_Item_Array, "notes", '')

...

 

Return Json Result:

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

 

Regards,

Ken

Comment
There are no comments made yet.
Kevin Ridley Accepted Answer Pending Moderation
  1. Monday, 11 June 2018 16:09 PM UTC
  2. PowerBuilder
  3. # 2

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.
Simone Olianti Accepted Answer Pending Moderation
  1. Monday, 11 June 2018 16:11 PM UTC
  2. PowerBuilder
  3. # 3

try this:

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

Comment
There are no comments made yet.
Govinda Lopez @Appeon Accepted Answer Pending Moderation
  1. Monday, 11 June 2018 21:14 PM UTC
  2. PowerBuilder
  3. # 4

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
  1. Andrew Davis
  2. Wednesday, 13 June 2018 18:12 PM UTC
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. Helpful
There are no comments made yet.
Alfredo Aldama Accepted Answer Pending Moderation
  1. Tuesday, 12 June 2018 16:22 PM UTC
  2. PowerBuilder
  3. # 5
Hello, Check how many levels nesting are possible with PB. Regards!
Comment
  1. Andrew Davis
  2. Wednesday, 13 June 2018 18:12 PM UTC
do you know how many levels are possible ?

  1. Helpful
There are no comments made yet.
Daniel Ferrreira Accepted Answer Pending Moderation
  1. Friday, 15 January 2021 20:09 PM UTC
  2. PowerBuilder
  3. # 6

Hi guys,

I'm not sure if I should post on a old thread, but it's that I'm experiencing the same as Andrew, the original poster. Please advice if I should create a new thread.

I think the "AddItemXXX" functions are bugged, when used with the "key" argument filled in, after AddItemArray. They return -1 and don't add the info. The PB example below, as posted by Govinda, works fine:

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)

but if I try to use the AddItemNumber with the "Key" info filled in, I get -1.

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, "myKey", 101)

Any thoughts on this? With the implementation above, the json is generated with "id": [], but the myKey child item doesn't appear. I'm using PB 2019 R2 2353.

Thanks!

Comment
  1. Daryl Foster
  2. Sunday, 17 January 2021 22:53 PM UTC
Hi Daniel, see my reply above
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Sunday, 17 January 2021 22:50 PM UTC
  2. PowerBuilder
  3. # 7

Hi Daniel,

You can't add a key directly to a JSON array item.  JSON arrays only support string, number, object, array, boolean or null.  So if you need that key value pair, you will need to wrap it in an object.  The return of -1 from AddItemNumber is correct in your code, not a bug, because it is an error to add a number with a key to an array.  The code below shows how to add an object to the array.  It will produce the following valid JSON:

{"id":[{"myKey":101}]}

Note that {"id":["myKey":101]} is not valid JSON

JsonGenerator lnv_JsonGenerator
Long ll_RootObject
Long ll_ChildArray
Long ll_ArrayItem

lnv_JsonGenerator = Create JsonGenerator

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

// Add an array child item
ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootObject, "id")

// Add an object to the array
ll_ArrayItem = lnv_JsonGenerator.AddItemObject(ll_ChildArray)
lnv_JsonGenerator.AddItemNumber(ll_ArrayItem, "myKey", 101)

Comment
There are no comments made yet.
Daniel Ferrreira Accepted Answer Pending Moderation
  1. Sunday, 17 January 2021 23:05 PM UTC
  2. PowerBuilder
  3. # 8

Thank you very much Daryl!!!!! You nailed it!

 

Comment
There are no comments made yet.
Malek Taha Accepted Answer Pending Moderation
  1. Friday, 6 January 2023 21:47 PM UTC
  2. PowerBuilder
  3. # 9

Than you very much Ken. You a "Life saver"

Thank you tahan you thank you.

It worked

Thank you

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.