JSON, Only Include Applied Data
Posted: Thu Feb 09, 2023 12:08 pm
Currently, the JSON functions such as InsertJSONStructure write every field of the structure to the JSON.
Will output:
Is there a simple way to only include data in the JSON that was inserted into the structure?
For an output like so:
We can use RemoveJSONMember and RemoveJSONElement but that quickly becomes really messy and line consuming once you start including in arrays, lists etc that hop down the line to many other structures from the main structure. Imo it becomes too big of a mess and hassle to rely on just to remove members or elements from the JSON that do not need to be included in the final result (mind you I'm dealing with large data objects where only small portions need to be included at any given point).
For example:
There's a lot of extra fields that we don't need included in the final JSON. These functions may work for logistics and other areas where empty values are expected but when dealing with websockets they can cause unexpected behavior as some servers mark many portions of objects as optional (including them, even with empty values triggers unexpected behavior).
It would be nice for:
To output:
Without needing to strip whatever is not used in the structure from the JSON manually.
Is there a way around this functionality that is simple and clean?
Code: Select all
Structure test
a.s
b.s
c.s
d.s
EndStructure
form.test
form\a = "Hello"
form\c = "World"
If CreateJSON(0)
InsertJSONStructure(JSONValue(0), @form, test)
Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
Code: Select all
{
"a": "Hello",
"b": "",
"c": "World",
"d": ""
}
For an output like so:
Code: Select all
{
"a": "Hello",
"c": "World"
}
For example:
Code: Select all
Structure info
a.i
b.i
c.i
EndStructure
Structure ad
Array a.info(0)
EndStructure
Structure test
a.s
b.s
c.s
Array d.ad(0)
EndStructure
form.test
form\a = "Hello"
form\c = "World"
form\d(0)\a(0)\a = 137
If CreateJSON(0)
InsertJSONStructure(JSONValue(0), @form, test)
Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
It would be nice for:
Code: Select all
Structure info
a.i
b.i
c.i
EndStructure
Structure ad
Array a.info(0)
EndStructure
Structure test
a.s
b.s
c.s
Array d.ad(0)
EndStructure
form.test
form\d(0)\a(0)\a = 137
If CreateJSON(0)
InsertJSONStructure(JSONValue(0), @form, test)
Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
Code: Select all
{
"d": [
{
"a": [
{
"a": 137
}
]
}
]
}
Is there a way around this functionality that is simple and clean?