Skip to content
Matthias Klatte edited this page Jan 24, 2015 · 1 revision

This library offers basic functionality to parse JSON strings.

Requirements
TextLib as TextLib

File
JSON.Script.txt

Usage

In this documentation I will use an example from http://json.org/example.

declare Text myJSONstring = """{
"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..."}
    ]
}
}""";

Parsing the Object

You begin parsing your string with JSON_Parse:

declare myJSONObject = JSON_Parse(myJSONstring);

Getting Values

Since there isn't much going on on the top level, we want to go deeper and select the object behind the key menu. We take the handle we got from JSON_Parse to get a value from this object. In our case again an object behind the key "menu".

declare menu = JSON_GetObject(myJSONObject, "menu");

Here we migth want to take a look at the keys in this object. JSON_Keys returns an array of all the key strings in the given object.

log(JSON_Keys(menu));
// [header, items]

As we now want to see, what is stored as header in the menu, we will simply take a look like this:

log(JSON_GetText(menu, "header"));

In the same way we can use the methods

  • JSON_GetInteger
  • JSON_GetReal
  • JSON_GetBoolean

Arrays

The key items stores an array of data, which we haven't encountered yet. We can get a handle on that with calling

declare itemsArray = JSON_GetArray(menu, "items");

The usual thing to do with arrays is to iterate over them. We get the array size with JSON_ArraySize like that:

for (i, 0, JSON_ArraySize(itemsArray) - 1) {
    // do something
}

As we saw before, the items array contains a bunch of objects, having a key id and sometimes a key label. Some array items are null. We should make sure, we don't try to access an object with an invalid handle! null values aren't objects, so we will avoid them. Now let's say we simply want to print all the ids and - if available - the associated labels on the console. Empty array items are interpreted as placeholders.

for (i, 0, JSON_ArraySize(itemsArray) - 1) {
    if (JSON_IsNull(itemsArray, i))
        log("/////////////////////////////////////");
    else {
        declare item = JSON_GetObject(itemsArray, i);
        log(JSON_GetText(item, "id"));
        if (JSON_HasKey(item, "label"))
            log("  " ^ JSON_GetText(item, "label"));
    }
}

As you see, the JSON_Get* functions are also available for arrays expecting an Integer offset instead of a Text key.

The output of the above will be

Open
OpenNew
  Open New
/////////////////////////////////////
ZoomIn
  Zoom In
ZoomOut
  Zoom Out
OriginalView
  Original View
/////////////////////////////////////
Quality
Pause
Mute
/////////////////////////////////////
Find
  Find...
FindAgain
  Find Again
Copy
CopyAgain
  Copy Again
CopySVG
  Copy SVG
ViewSVG
  View SVG
ViewSource
  View Source
SaveAs
  Save As
/////////////////////////////////////
Help
About
  About Adobe CVG Viewer...

just as expected.

Loglevel

Before executing your functions you can set the log level with

JSON_LogLevel = LOGLEVEL_LOGLEVEL_WARN;

You can use the constants JSON_LOGLEVEL_NONE < JSON_LOGLEVEL_ERROR < JSON_LOGLEVEL_WARN < JSON_LOGLEVEL_DEBUG.

Available Functions

  • Text JSON_Parse(Text _Input)
    Returns a handle on a parsed JSON Object/Array
  • Text JSON_GetType(Text _Instance)
    Returns a string equal to JSON_TYPE_OBJECT or JSON_TYPE_ARRAY
  • Boolean JSON_IsObject(Text _Instance)
  • Boolean JSON_IsArray(Text _Instance)

Object functions

  • Boolean JSON_HasKey(Text _Instance, Text _Key)
  • Text[] JSON_Keys(Text _Instance)

Array functions

  • Integer JSON_ArraySize(Text _Instance)

Mixed functions

(second argument is Text for objects, Integer for arrays)

  • Text JSON_GetType(Text _Instance, Text _Key)
    The returned value will equal JSON_TYPE_OBJECT, JSON_TYPE_ARRAY, JSON_TYPE_STRING, JSON_TYPE_NUMBER or JSON_TYPE_CONST
  • Text JSON_GetObject(Text _Instance, Text _Key)
    Returns an object handle.
  • Text JSON_GetArray(Text _Instance, Text _Key)
    Returns an array handle.
  • Text JSON_GetText(Text _Instance, Text _Key)
  • Boolean JSON_GetBoolean(Text _Instance, Text _Key)
  • Boolean JSON_IsNull(Text _Instance, Text _Key)
  • Integer JSON_GetInteger(Text _Instance, Text _Key)
    Note that the TextLib can't handle exponents in strings, so you might want to use GetText and handle exponents yourself
  • Real JSON_GetReal(Text _Instance, Text _Key)
    Note that the TextLib can't handle exponents in strings, so you might want to use GetText and handle exponents yourself

Since most functions should be pretty self-explanatory they are not described in too much detail, you might consider risking a look into the implementations docs.

You can’t perform that action at this time.