Sets an element's value.

Syntax

SRP_JsonX_Set(Path, Value, Hint)

Returns

1 if successful, 0 if there was an error.

Parameters

ParameterDescription
PathPath to the element whose value to set.
ValueThe new value. Omit to pass null.
HintForces the value into a json type. Can be "Bool", "Null", or "String". Optional. 

Remarks

SRP_JsonX_Set sets the value of the element pointed to by Path. See Paths for more details. If the element doesn't exist, it is created. The difference between this routine and SRP_Jsonx is that this routine does not change the state of the document when called. It is better suited for editing existing documents whereas SRP_JsonX is better suited for building documents from scratch.

The Value parameter will be smartly interpreted by the routine unless a Hint is specified. OI numbers are converted into json numbers. You can also pass json as the value, and this routine will parse the json and set it as the element's new value.

The Hint parameter can be used to force the value into a specific json type. Use "Bool" to force the value to be true or false. Use "Null" to ignore the given value and just use null as the value. Use "String" to force the value into a string, which is useful when you want an OI number to be surrounded by quotes in the json itself.

Examples

$insert SRPJSONX
 
Json  = '{'
Json := '    "employees": ['
Json := '        {'
Json := '            "firstname": "John",'
Json := '            "lastname": "Doe",'
Json := '            "age": 21'
Json := '        },'
Json := '        {'
Json := '            "firstname": "Anna",'
Json := '            "lastname": "Smith",'
Json := '            "age": 32'
Json := '        },'
Json := '        {'
Json := '            "firstname": "Peter",'
Json := '            "lastname": "Jones",'
Json := '            "age": 43'
Json := '        }'
Json := '    ],'
Json := '    "nums": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],'
Json := '    "active": true,'
Json := '    "alwaysnull": null,'
Json := '    "alwaysstring": "4.321"'
Json := '}'
 
SRP_JsonX_Parse('MyDocument', json)

    // Change an existing value
    SRP_JsonX_Set('employees[1].age', 54)
 
    // Set a value that doesn't exist yet
    SRP_JsonX_Set('new', 'hello')
 
NewJson = SRP_JsonX_End('Pretty')
  • No labels