JSON, Only Include Applied Data

Just starting out? Need help? Post your questions and find answers here.
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

JSON, Only Include Applied Data

Post by Opcode »

Currently, the JSON functions such as InsertJSONStructure write every field of the structure to the JSON.

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
Will output:

Code: Select all

{
  "a": "Hello",
  "b": "",
  "c": "World",
  "d": ""
}
Is there a simple way to only include data in the JSON that was inserted into the structure?

For an output like so:

Code: Select all

{
  "a": "Hello",
  "c": "World"
}
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:

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
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:

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
To output:

Code: Select all

{
  "d": [
      {
        "a": [
            {
              "a": 137
            }
          ]
      }
    ]
}
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?
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: JSON, Only Include Applied Data

Post by infratec »

I think there is no easy way.

The main problem is: how can a program decide if "" is not needed or it is used to clear something.

I think the easiest way is to build the JSON string by yourself.
wayne-c
Enthusiast
Enthusiast
Posts: 337
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: JSON, Only Include Applied Data

Post by wayne-c »

maybe off-topic but in Go it's implemented like this:

Code: Select all

type test struct {
	a string
	b string `json:",omitempty"`
}
a will always be present, also ""; b will only be present in the JSON when != ""

In PB we might have an additional parameter in ComposeJSON : #PB_JSON_OmitEmpty ?
As you walk on by, Will you call my name? Or will you walk away?
Post Reply