How to create JSON format below

Hi,

We would like to create JSON in the below format. We are able to create the first part of the JSON but we doe not now how to create the part that starts with “Objects”.

{
“KnSubject”: {
“Element”: {
“Fields”: {
“StId”: 57,
“Ds”: “Topic”,
“FvF1”: 100,
“SbPa”: “Sample.jpg”,
“FileTrans”: true,
“FileStream”: “/9j/4AAQSkZJR…”
},
“Objects”: [
{
“KnSubjectLink”: {
“Element”: {
“Fields”: {
“DoCRM”: true,
“ToBC”: true,
“SfTp”: 3,
“SfId”: “999”,
“BcId”: “9999999”
}
}
}
}
]
}
}
}

@Petra
https://jsonlint.com/ is one of the online validator and formater tools for JSON and can help for developing a strategy

  • Create a template JOBject with the properties
  • add to the properties the different values or other JObjects / JArray

Herefore the NewtonSoft API helps

Hi Peter, We have created the JOBject and added the properties. But we did not manage toe create the Jarray with the Square Bracket as shown in the JSON. That is the part we are stuck right now.

low level constructing up from bottom to top:

  • create a Jobject - var jo1:
    {
    “DoCRM”: true,
    “ToBC”: true,
    “SfTp”: 3,
    “SfId”: “999”,
    “BcId”: “9999999”
    }
  • create a Jobject - var jo2 and set the value of property Fields to jo1
  • create a jObject - var jo3 and set the value of property Element to jo2
  • similar for KnSubjectLink - jo3
  • create a JArray - new JArray(… and add jo3 to it - ja1
  • set the value of the property Objects to ja1

I find it easiest to use an invoke code. I would use something like below to create a Jarray, create some objects and add to the Jarray as items. Finally add the jarray back into the Jobject you want it to be in (assuming that property is already created)

'Create Jarray
out_FileJarray = New Jarray

'create new jobjects
Dim FileJobject1 As New JObject
Dim FileJobject2 As New JObject

'Add properties to the Jobjects
FileJobject1.Add(New JProperty("filePath","file path1"))
FileJobject1.Add(New JProperty("name","file name1"))
FileJobject2.Add(New JProperty("filePath","file path2"))
FileJobject2.Add(New JProperty("name","file name2"))

'Add objects to array
 out_FileJarray.Add(FileJobject1)
 out_FileJarray.Add(FileJobject2)
 
 'Add Jarray back into the original Jobject
 OriginalJObject.Item("ArrayProperty") =DirectCast(out_FileJarray,JToken)