Page History
...
To get the final json output, we call SRP_JsonX_End. This routine does two things. It optionally returns the current document as a json string, then it removes the current document from memory and makes the previous document on the stack the current one.
Glossary
Document: An active JsonX data structure in memory.
...
Code Block | ||||
---|---|---|---|---|
| ||||
SRP_JsonX_BeginString('MyDocument', '{', JsonxPretty$)
SRP_JsonX('employees', '[')
SRP_JsonX('{')
SRP_JsonX('firstname', 'John') ; // Since we're in an object, the first parameter is a member name and the second is its value
SRP_JsonX('lastname', 'Doe')
SRP_JsonX('age', 21)
SRP_JsonX('}')
SRP_JsonX('{')
SRP_JsonX('firstname', 'Anna')
SRP_JsonX('lastname', 'Smith')
SRP_JsonX('age', 32)
SRP_JsonX('}')
SRP_JsonX('{"firstname":"Peter", "lastname":"Jones", "age":43}')
SRP_JsonX(']')
SRP_JsonX('count', 4)
SRP_JsonX('active', 1, 'Bool')
SRP_JsonX('alwaysnull')
SRP_JsonX('alwaysstring', 4.321, 'String')
Json = SRP_JsonX_End() |
The above example makes the exact same json as Example 1, but this time we started with SRP_JsonX_BeginString. Note how the calls to SRP_JsonX don't behave any differently. The only difference is that behind the scenes, SRP JsonX is producing pretty json text directly. If you know you are making a json string and that you can build it from the top down, then SRP_JsonX_BeginString will be faster.
Parsing Json
SRP JsonX is the fastest parser available to OI developers, and now it's easier to navigate, modify, and extract json documents thanks to the elimination of cumbersome handles. Let's parse the following json:
Code Block | ||
---|---|---|
| ||
{
"employees": [
{
"firstname": "John",
"lastname": "Doe",
"age": 21
},
{
"firstname": "Anna",
"lastname": "Smith",
"age": 32
},
{
"firstname": "Peter",
"lastname": "Jones",
"age": 43
},
],
"nums": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"alwaysbool": true,
"alwaysnull": null,
"alwaysstring": "4.321"
} |
Code Block | ||||
---|---|---|---|---|
| ||||
$insert SRPJSONX
// Create a new document by parsing the json text
SRP_JsonX_Parse('MyParsedDoc', Json)
// Get the first employee's last name using SRP JsonX path syntax
LastName = SRP_JsonX_Get('employees[1].lastname')
// If our path points to an object or an array, we'll get the entire object or array as json text
Employee = SRP_JsonX_Get('employees[1]')
// Routines the get information don't change the current path. If we want to change the
// current path, we use the Go routines. Let's make the second employee the current path.
SRP_JsonX_Go('employees[2]')
// Now, if I want to the last name, I must use a path relative to the current one
LastName = SRP_JsonX_Get('lastname')
// If I try to use a full path, I'll get a null value because all paths are relative to the current one
LastName = SRP_JsonX_Get('employees[3].lastname')
// We can go back up the json heirarchy one level at a time. This call makes the current path 'employees'
SRP_JsonX_GoBack()
// Now I can get the third employee's last name
LastName = SRP_JsonX_Get('[3].lastname')
// We can get make the root the current path at any time
SRP_JsonX_GoRoot()
// When a value is null or not found, '' is returned by default. If you want a different value,
// then set the DefaultIfNull parameter to your desired default
Value = SRP_JsonX_Get('alwaysnull', 'This is null!!!')
// We can also query the json for information (remember, we are currently at the root)
NumEmployees = SRP_JsonX_Count('employees')
MemberExists = SRP_JsonX_Has('employees[2].age')
MemberNames = SRP_JsonX_Members('employees[3]')
Type = SRP_JsonX_Type('alwaysbool')
Numbers = SRP_JsonX_Value('nums', ',')
// We can also modify the json
SRP_JsonX_Clear('nums')
SRP_JsonX_Delete('employees[2]')
SRP_JsonX_Sort()
// All done with our parsed document.
SRP_JsonX_End() |
Example 3 gives you an idea of all the options available to when it comes to reading and modifying json.
Debugging
Given that SRP JsonX relies so heavily on state, it's important to give you the tools you need to troubleshoot issues. If you want to view the current state of SRP JsonX, call the SRP_JsonX_State or SRP_JsonX_Trace. Both of these show you the current stack and the current paths for each document on the stack. The difference between the two is that SRP_JsonX_Trace displays a message box whereas SRP_JsonX_State returns the stack as an @FM delimited array suitable for viewing in the debugger.
When an SRP JsonX method fails, you can call SRP_JsonX_Error immediately afterwards to get the error message.
Reference
Here are links to all the SRP JsonX functions.
Routine | Description |
---|---|
SRP_JsonX | Adds json elements to the current document based on the current path. |
SRP_JsonX_Begin | Creates a new json document. |
SRP_JsonX_BeginString | Creates a new json document that must be built in order. |
SRP_JsonX_Clear | Deletes all elements in an object or array. |
SRP_JsonX_Count | Gets the number of elements in an object or array. |
SRP_JsonX_Delete | Deletes an element and all it's children. |
SRP_JsonX_End | Ends the current document, optionally returning json. |
SRP_JsonX_Error | Gets the last known error. |
SRP_JsonX_Get | Gets an elements value or json. |
SRP_JsonX_Go | Makes an object or array the current path. |
SRP_JsonX_GoBack | Makes the parent of the current path the new current path. |
SRP_JsonX_GoRoot | Makes the document root the current path. |
SRP_JsonX_Has | Determines if an element exists. |
SRP_JsonX_Members | Gets all an object's member names. |
SRP_JsonX_Parse | Opens a new json document. |
SRP_JsonX_Set | Sets a value or json. |
SRP_JsonX_Sort | Sorts an object's members by member name. |
SRP_JsonX_State | Gets the state of the stack and all its documents. |
SRP_JsonX_Trace | Shows the state of the stack and all its documents. |
SRP_JsonX_Type | Gets an element's type. |
SRP_JsonX_Value | Gets all an object's or array's values. |