Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

  • Insert a string "John" as the "name" property of a JSON object
    #RootObject.InsertString Key('name') String('John')
  • Insert a number 9 into a JSON array
    #JsonArray.InsertNumber Number(9)
  • Insert a NULL value as the "allergies" property of a JSON object
    #RootObject.InsertNull Key('allergies')
    The following example show how to create an object/array as a property of another object:
  • Define an XPRIM_JsonObject reference
    Define_Com Class(#XPRIM_JsonObject) Name(#ChildObject)
  • Insert the street & suburb of the address into #ChildObject
    #ChildObject.InsertString Key('street') String('122 Arthur St')
    #ChildObject.InsertString Key('suburb') String('North Sydney')
  • Insert #ChildObject into #RootObject as a member with name "address"
    #RootObject.InsertElement Key('address') Element(#ChildObject)
    If you want to convert your XPRIM_JsonObject (or XPRIM_JsonArray) into a string (for example for debugging purpose), call the SerializeAsString method.
    Define_Com Class(#PRIM_DC.UnicodeString) Name(#MyJsonString)
  • Convert JSON to a string
    #MyJsonString := #RootObject.SerializeAsString
    You can also serialize your JSON object (or array) into a file, using the SerializeToFile method.
    #RootObject.SerializeToFile Path('/home/TestJson.txt')
    You can also parse a JSON string and construct an XPRIM_JsonObject or XPRIM_JsonArray from the JSON string, using the ParseString and ParseFile methods.
  • Define an ErrorInfo object to hold the error details
    Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorInfo)
  • Call the 'Parse' method
    #RootObject.ParseString String(#JsonString) ErrorInfo(#ErrorInfo)
  • Get the error message if parsing failed
    If (*Not #ErrorInfo.OK)
        #ErrorMessage := #ErrorInfo.ErrorMessage
    EndIf
    You can access the values of an object or an array using one of the Get… accessor methods:
  • GetString
  • GetBoolean
  • GetNumber
  • GetObject
  • GetArray

Wiki Markup
For example, if you have a JSON string like this, stored in a variable called '#String':
\{
    "name":      \{ 
        "given": "John",
        "surname": "Smith"
    \},
    "age": 45,
    "contactNo":
    \[
        \{ area: "02", no: "9378 2867", type: "landline" \},
        \{ no: "0468 732 371", type: "mobile" \}
    \]
\}
To parse this JSON string and read its values:
Define_Com Class(#XPRIM_JsonObject) Name(#RootObject)
Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorInfo)

  • Parse JSON string
    #RootObject.ParseString String(#String) ErrorInfo(#ErrorInfo)
  • First check thst the JSON string was parsed OK
    If (#ErrorInfo.OK)
        * Get the values
        #GivenName := #RootItem.GetObject('name').GetString('given')
        #Surname := #RootItem.GetObject('name').GetString('surname')
        #Age := #RootItem.GetNumber('age')
    Endif
    You can use the ItemCount property to tell how many item an array (or object) contains.
    Define_Com Class(#XPRIM_JsonArray) Name(#ContactNumbers)
  • Get the contactNo value (which is an array), ...
  • ... and assign to #ContactNumbers
    #ContactNumbers.Refer To(#RootItem.GetArray('contactNo')
  • Get the array length using the 'ItemCount' property
    #ContactCount := #ContactNumbers.ItemCount
    To iterate through elements in a JSON array, you can use the FOR-EACH-IN construct:
  • Use the contactNo array from the previous example
    FOR EACH(#Number) IN(#ContactNumbers)
       #COM_SELF.ProcessContactNumber Number(#Number)
    ENDFOR
    You can check the type of a JSON element (XPRIM_JsonElement) by reading its Type property, which will return one of the following values:
  • NULL
  • STRING
  • NUMBER
  • BOOLEAN
  • OBJECT
  • ARRAY

...