Hi Everyone,
I've got a question about how people are creating complex JSON for real world applications. I'm updating an application to use a third party's REST API and some of the API calls require reasonably complex JSON to be sent. The JSON may have embedded objects and/or arrays. An example is something similar to:
{
"yourReference": "123456",
"amendments": {
"drawings": [
{
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e",
"scope": "REPLACE"
}
],
"tableOfContents": [
{
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e",
"scope": "CLEAN"
}
],
"index": [
{
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e",
"scope": "REPLACE"
}
],
"description": [
{
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e",
"scope": "MARKED_UP"
},
{
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e",
"scope": "CLEAN"
}
],
"abstract": [
{
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e",
"scope": "MARKED_UP"
}
]
},
"summaryDocument": {
"identifier": "86073b67-60b1-4495-9001-7ea650b7e63e"
},
"submittedWithPayment": false
}
For the calls that require simple JSON I've been using either the export function from a Datastore or the MSSQL JSON functions to generate it. For something more complex I'm looking at using JSONGenerator. A simple static example to generate the JSON above is:
JSONGenerator lnv_JsonGenerator
integer li_root
integer li_amendments
integer li_summary
integer li_array
integer li_document
string ls_json
lnv_JsonGenerator = create JSONGenerator
li_root = lnv_JsonGenerator.CreateJsonObject()
lnv_JsonGenerator.AddItemString(li_root, "yourReference", "123456")
li_amendments = lnv_JsonGenerator.AddItemObject(li_root, "amendments")
li_array = lnv_JsonGenerator.AddItemArray(li_amendments, "drawings")
li_document = lnv_JsonGenerator.AddItemObject(li_array)
lnv_JsonGenerator.AddItemString(li_document, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemString(li_document, "scope", "REPLACE")
li_array = lnv_JsonGenerator .AddItemArray(li_amendments, "tableOfContents")
li_document = lnv_JsonGenerator .AddItemObject(li_array)
lnv_JsonGenerator.AddItemString(li_document, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemString(li_document, "scope", "CLEAN")
li_array = lnv_JsonGenerator.AddItemArray(li_amendments, "index")
li_document = lnv_JsonGenerator.AddItemObject(li_array)
lnv_JsonGenerator.AddItemString(li_document, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemString(li_document, "scope", "REPLACE")
li_array = lnv_JsonGenerator.AddItemArray(li_amendments, "description")
li_document = lnv_JsonGenerator.AddItemObject(li_array)
lnv_JsonGenerator.AddItemString(li_document, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemString(li_document, "scope", "MARKED_UP")
li_document = lnv_JsonGenerator.AddItemObject(li_array)
lnv_JsonGenerator.AddItemString(li_document, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemString(li_document, "scope", "CLEAN")
li_array = lnv_JsonGenerator.AddItemArray(li_amendments, "abstract")
li_document = lnv_JsonGenerator.AddItemObject(li_array)
lnv_JsonGenerator.AddItemString(li_document, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemString(li_document, "scope", "MARKED_UP")
li_summary = lnv_JsonGenerator.AddItemObject(li_root, "summaryDocument")
lnv_JsonGenerator.AddItemString(li_summary, "identifier", "86073b67-60b1-4495-9001-7ea650b7e63e")
lnv_JsonGenerator.AddItemBoolean(li_root, "submittedWithPayment", false)
ls_json = lnv_JsonGenerator.GetJsonString()
I know I can tidy that code up and maybe use loops or functions to create the document arrays, but is this the general method for creating complex JSON. Does anyone have any simpler suggestions? There will be quite a few different API calls I will need to make and many of them have complex JSON that will need to be generated so I want to make sure I'm on the right track before progressing too far. Obviously the example above is just using static data, the application will be getting the required data from a database, probably via one or more datastores.
Any suggestions would be welcome.