Page 1 of 1

creating json V15

Posted: Fri Dec 13, 2024 2:01 am
by kno_dk
Hi

I have this function where I create a json file with some info regardin a shipment. It works nearly ok.

I have a problem in the parcel array which give me this:
"parcels": [
{
"weight": 2000
},
{
"weight": 2000
}
]

it should have been one with 1000 and one with 2000.
Here is my code - hope it is a simpel solution.


Function Options(*DIRECT)
Define_Com Class(#XPRIM_Json) Name(#json)
Define_Com Class(#XPRIM_JsonObject) Name(#jsonObj) Reference(*DYNAMIC)
Define_Com Class(#XPRIM_JsonObject) Name(#jsonSenderObject)
Define_Com Class(#XPRIM_JsonObject) Name(#jsonReceiverObject)
Define_Com Class(#XPRIM_JsonObject) Name(#jsonParcelObject)
*
Define_Com Class(#XPRIM_JsonArray) Name(#jsonparcelArray) Reference(*DYNAMIC)


#jsonObj <= #json.CreateRootObject
*
#jsonObj.InsertBoolean Key('test_mode') Boolean(true)
*
#jsonObj.InsertBoolean Key(own_agreement) Boolean(false)
#jsonObj.InsertString Key('label_format') String('a4_pdf')
#jsonObj.InsertString Key('product_code') String('GLSDK_SD')
#jsonObj.InsertString Key('service_codes') String('EMAIL_NT,SMS_NT')
#jsonObj.InsertString Key('reference') String('Order 10001')
#jsonObj.InsertBoolean Key('automatic_select_service_point') Boolean(true)

* sender
#jsonSenderObject.InsertString Key('name') String('Min Virksomhed ApS')
#jsonSenderObject.InsertString Key('attention') String('Lene Hansen')
#jsonSenderObject.InsertString Key('address1') String('adr 1')
#jsonSenderObject.InsertString Key('zipcode') String('5220')
#jsonSenderObject.InsertString Key('city') String('Odense SØ')
#jsonSenderObject.InsertString Key('country_code') String('DK')
#jsonSenderObject.InsertString Key('email') String('info@minvirksomhed.dk')
#jsonSenderObject.InsertString Key('mobile') String('70Xxxxx07')
#jsonobj.InsertElement Element(#jsonSenderObject) Key('sender')

* receiver
#jsonReceiverObject.InsertString Key('name') String('Lene Hansen')
#jsonReceiverObject.InsertString Key('address1') String('Radr1')
#jsonReceiverObject.InsertString Key('zipcode') String('5010')
#jsonReceiverObject.InsertString Key('city') String('Odense X')
#jsonReceiverObject.InsertString Key('country_code') String('DK')
#jsonReceiverObject.InsertString Key('email') String('lene@xmail.dk')
#jsonReceiverObject.InsertString Key('mobile') String('12345678')
#jsonobj.InsertElement Element(#jsonReceiverObject) Key('receiver')

* parcel
#jsonparcelArray <= #jsonobj.InsertArray( 'parcels' )

#jsonParcelObject.Insertnumber Key('weight') Number(1000)
#jsonparcelArray.InsertElement Element(#jsonParcelObject)

#jsonParcelObject.Insertnumber Key('weight') Number(2000)
#jsonparcelArray.InsertElement Element(#jsonParcelObject)

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

Return

Re: creating json V15

Posted: Mon Jan 13, 2025 10:35 am
by Tim McEntee
Hi

Try

DEFINE_COM Class(#PRIM_WEB.JsonObject) Name(#jsonParcelObjectArrayItem) Reference(*DYNAMIC)

#jsonparcelArray <= #jsonobj.InsertArray( 'parcels' )

#jsonParcelObjectArrayItem <= #jsonparcelArray.InsertObject
#jsonParcelObjectArrayItem.InsertString Key('weight') Number(1000)

#jsonParcelObjectArrayItem <= #jsonparcelArray.InsertObject
#OutputObjectArrayItem.InsertString Key('weight') Number(2000)

Tim