creating json V15

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
kno_dk
Posts: 207
Joined: Tue Feb 23, 2016 12:00 am

creating json V15

Post by kno_dk » Fri Dec 13, 2024 2:01 am

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

Tim McEntee
Posts: 49
Joined: Thu May 26, 2016 8:46 am

Re: creating json V15

Post by Tim McEntee » Mon Jan 13, 2025 10:35 am

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

Post Reply