Page 1 of 1

#XPRIM_Json: Create Json with two arrays

Posted: Thu Apr 21, 2022 5:23 pm
by René Houba
Because there is no online #XPRIM_Json documentation available yet, I like to introduce some examples here. I hope that based on my examples, that others will follow and share their examples here as well.

This example creates a Json file with two arrays.

* Example with two Array's
Define_Com Class(#XPRIM_Json) Name(#json)
Define_Com Class(#XPRIM_JsonObject) Name(#jsonObj) Reference(*DYNAMIC)
Define_Com Class(#XPRIM_JsonObject) Name(#jsonNameObject) Reference(*DYNAMIC)

Define_Com Class(#XPRIM_JsonArray) Name(#jsonNamesArray) Reference(*DYNAMIC)
Define_Com Class(#XPRIM_JsonArray) Name(#jsonEmailArray) Reference(*DYNAMIC)

* Create root for JSON document
#jsonObj <= #json.CreateRootObject

* Create array for names
#jsonNamesArray <= #jsonObj.InsertArray( 'names' )

* Add name object, first name
#jsonNameObject <= #jsonNamesArray.InsertObject
* Add 1st level data in json name object
#jsonNameObject.InsertString Key("givenname") String('Peter')
#jsonNameObject.InsertString Key("lastname") String("Smith")
#jsonNameObject.InsertNumber Key('age') Number(58)

* Add email array into name object
#jsonEmailArray <= #jsonNameObject.InsertArray( 'email' )
#jsonEmailArray.InsertString String('[email protected]')
#jsonEmailArray.InsertString String('[email protected]')

* Add name object, second name
#jsonNameObject <= #jsonNamesArray.InsertObject
* Add 1st level data in json name object
#jsonNameObject.InsertString Key("givenname") String('Susan')
#jsonNameObject.InsertString Key("lastname") String("Walker")
#jsonNameObject.InsertNumber Key('age') Number(25)

* Add email array into name object
#jsonEmailArray <= #jsonNameObject.InsertArray( 'email' )
#jsonEmailArray.InsertString String('[email protected]')
#jsonEmailArray.InsertString String('[email protected]')

#jsonObj.SerializeToFile Path('C:\TEMP\JsonExampleWithTwoArrays.json')


Result:
Create Json with two arrays.PNG
Create Json with two arrays.PNG (13.15 KiB) Viewed 42545 times