Returns a descendant JSON Entity within a given JSON Entity.

Syntax

Result = SRP_Json(Handle, "Get", Path)

Returns

A valid JSON Entity Handle if the descendant was found, 0 if not.

Parameters

ParameterDescription
HandleHandle to a JSON Entity. Required.
PathA formatted string indicating the desired descendant. Required.

Remarks

The Get service returns a JSON Entity Handle to a descendant entity somewhere within the current JSON Entity Handle. Handle must point to a JSON object or JSON array for this method to work. Otherwise, it just returns the entity's current value.

The term descendant is intentional because you can extract members or entities deep within the JSON tree in a single call. Let's use the following JSON as an example:

{"menu": {
    "header": "SVG Viewer",
    "items": [
        {"id": "Open"},
        {"id": "OpenNew", "label": "Open New"},
        null,
        {"id": "ZoomIn", "label": "Zoom In"},
        {"id": "ZoomOut", "label": "Zoom Out"},
        {"id": "OriginalView", "label": "Original View"},
        null,
        {"id": "Quality"},
        {"id": "Pause"},
        {"id": "Mute"},
        null,
        {"id": "Find", "label": "Find..."},
        {"id": "FindAgain", "label": "Find Again"},
        {"id": "Copy"},
        {"id": "CopyAgain", "label": "Copy Again"},
        {"id": "CopySVG", "label": "Copy SVG"},
        {"id": "ViewSVG", "label": "View SVG"},
        {"id": "ViewSource", "label": "View Source"},
        {"id": "SaveAs", "label": "Save As"},
        null,
        {"id": "Help"},
        {"id": "About", "label": "About Adobe CVG Viewer..."}
    ]
}}

In this example, we have a root object with a single child object called "menu" as it's only member. That object contains a member called items, which is an array, and each element in the array is either null or a child object. As you can see, it would be cumbersome to extract a single descendant as it would require getting a child handle, followed by another child handle, and so on. That is why the GET service supports a path.

The path is a specially formatted string that instructs the service how to drill down into the JSON. If you simply pass a single member name or index, then you'll simply get the handle to that object. For example, we can get the "menu" object like so:

MenuHandle = SRP_Json(ObjectHandle, "Get", "menu")

If you want to get a child of menu (without having to get the menu handle first), you separate each member name with a period, like so:

ItemsHandle = SRP_Json(ObjectHandle, "Get", "menu.items")

If you want to get an element within an array, you use square brackets instead of periods. This is how we drill down directly to the twelfth element within the items array:

ItemHandle = SRP_Json(ObjectHandle, "Get", "menu.items[12]")

And this is how we get the handle to the "label" member of the second item in the items array:

LabelHandle = SRP_Json(ObjectHandle, "Get", "menu.items[2].label")

Important. The Path parameter is a convenient way to drill into a JSON document. It reserves five characters: period, left square bracket, right square bracket, left angle bracket, and right angle bracket. If the member you want to access uses one of these characters, you must escape them by doubling up. Here's an example of a JSON document with periods in the member names and how to access them.

{
    "first.name" : "John"
    "last.name"  : "Doe"
}


// INCORRECT: This will get you "" because it will try to get "first" and then "name"
Handle = SRP_Json(ObjectHandle, "Get", "first.name")


// CORRECT: This will get you "first.name"
Handle = SRP_Json(ObjectHandle, "Get", "first..name")


This service always returns a JSON Entity Handle. If you know the descendant will be a String, Number, or Boolean type entity, you might prefer to use GetValue instead, which gets the value instead of the handle.

Example

// Test parsing an existing JSON string and then drilling down into specific values
SampleJSON  = '{"menu": {'
SampleJSON := '    "header": "SVG Viewer",'
SampleJSON := '    "items": ['
SampleJSON := '        {"id": "Open"},'
SampleJSON := '        {"id": "OpenNew", "label": "Open New"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "ZoomIn", "label": "Zoom In"},'
SampleJSON := '        {"id": "ZoomOut", "label": "Zoom Out"},'
SampleJSON := '        {"id": "OriginalView", "label": "Original View"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "Quality"},'
SampleJSON := '        {"id": "Pause"},'
SampleJSON := '        {"id": "Mute"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "Find", "label": "Find..."},'
SampleJSON := '        {"id": "FindAgain", "label": "Find Again"},'
SampleJSON := '        {"id": "Copy"},'
SampleJSON := '        {"id": "CopyAgain", "label": "Copy Again"},'
SampleJSON := '        {"id": "CopySVG", "label": "Copy SVG"},'
SampleJSON := '        {"id": "ViewSVG", "label": "View SVG"},'
SampleJSON := '        {"id": "ViewSource", "label": "View Source"},'
SampleJSON := '        {"id": "SaveAs", "label": "Save As"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "Help"},'
SampleJSON := '        {"id": "About", "label": "About Adobe CVG Viewer..."}'
SampleJSON := '    ],'
SampleJSON := '    "testbool": "true",'
SampleJSON := '    "testint": "1234567890",'
SampleJSON := '    "testreal": "1234567890.987654321"'
SampleJSON := '}}'

 // Parse it, then grab the label of entity 12 within the items array
If SRP_Json(ObjectHandle, "Parse", SampleJSON) EQ "" then
   LabelHandle = SRP_Json(ObjectHandle, "Get", "menu.items[12].label")
   SRP_Json(ObjectHandle, "Release")
end

See Also

GetValue

  • No labels