Json how to add square brackets[ ]

I need to have square brackets around part of my json. How do I do that? Here is the code.

//construct the json
Var pa_request as New JSONItem

Var passport As New JSONItem
passport.Value(“accesskey”) = “MySecretKey”
passport.Value(“account”) = “MyAcccount”
passport.Value(“password”) = “”
passport.Value(“requestkey”) = “Test3”
passport.Value(“username”) = “”

Var items As New JSONItem
items.Value(“id”) = “MyId”
items.Value(“itemnumber”) = “HY272041”
items.Value(“quantity”) = 1
items.Value(“reference”) = “TestRef”
items.Value(“wh”) = 1

// Add to the pa_request object
pa_request.Value(“items”) = items
pa_request.Value(“passport”) = passport

Here is how the Items section should look with the square brackets.

“items”:[{
“id”:“MyId”,
“itemnumber”:“HY272041”,
“quantity”:1,
“reference”:“TestRef”,
“wh”:1
}],

each item in { } is one “object” worth of data

an array of objects is surrounded in [ ]

so you should use the ARRAY method(s) to add the items to pa_request

(how you do that varies slightly between older versions 2019r1.1 and older and newer versions 2019r2 and newer)

1 Like

Thanks Norman, I will give that a try.

Change your items variable to item and then make a new items variable like this:

Var items as new JSONItem
Items.addrow(item)

Here is how I did it. It works great. Thanks for help.

//construct the json
Var pa_request as New JSONItem

Var passport As New JSONItem
passport.Value(“accesskey”) = TfKey.Value //Gets the text input from the textbox
passport.Value(“account”) = TfAcct.Value //Gets the text input from the textbox
passport.Value(“password”) = “”
passport.Value(“requestkey”) = “Test3”
passport.Value(“username”) = “”

Var items As New JSONItem

Var mypart as New JSONItem
mypart.value(“id”) = “TestId”
mypart.Value(“itemnumber”) = TfPart.Value //Gets the text input from the textbox
mypart.Value(“quantity”) = 1
mypart.Value(“reference”) = “TestRef”
mypart.Value(“wh”) = 1
items.Add(mypart)

// Add to the pa_request object
pa_request.Value(“items”) = items
pa_request.Value(“passport”) = passport

1 Like

Var items As New JSONItem("[]")