v2026.11.3
All Bundles
Bundle

JSON parsing and building library. JsonParser reads JSON strings into a JsonElement tree; JsonBuilder constructs JSON documents using a stack-based API. Compile with -lib json.

JsonElement

JSON value element

Example

json := JsonParser->TextToElement("{\"name\": \"John\", \"age\": 30}");
if(json <> Nil) {
  name := json->Get("name");   # Nil for a missing member
  if(name <> Nil) {
    name->GetString()->PrintLine();
  };

  age := json->Get("age");
  if(age <> Nil) {
    age->GetInt()->PrintLine();
  };
};

# create JSON programmatically
obj := JsonElement->New(JsonElement->JsonType->OBJECT);
obj->Insert("city", "Oakland");
obj->Insert("zip", 94612);
obj->ToString()->PrintLine();

Operations

Add #

Adds an element

method : public : Add(elem:JsonElement) ~ Bool

Parameters

NameTypeDescription
elemJsonElementelement

Return

TypeDescription
Booltrue if successful, false otherwise

Example

arr := Data.JSON.JsonElement->New(Data.JSON.JsonElement->JsonType->ARRAY);
item := Data.JSON.JsonElement->New(Data.JSON.JsonElement->JsonType->NUMBER, "42");
arr->Add(item);
arr->Size()->PrintLine();  # 1

Add #

Adds a value

method : public : Add(value:String) ~ Bool

Parameters

NameTypeDescription
valueStringvalue

Return

TypeDescription
Booltrue if successful, false otherwise

Example

arr := Data.JSON.JsonElement->New(Data.JSON.JsonElement->JsonType->ARRAY);
arr->Add("apple");
arr->Add("banana");
arr->ToString()->PrintLine();

Add #

Adds a value

method : public : Add(value:Int) ~ Bool

Parameters

NameTypeDescription
valueIntvalue

Return

TypeDescription
Booltrue if successful, false otherwise

Add #

Adds a value

method : public : Add(value:Float) ~ Bool

Parameters

NameTypeDescription
valueFloatvalue

Return

TypeDescription
Booltrue if successful, false otherwise

Add #

Adds a value

method : public : Add(value:Bool) ~ Bool

Parameters

NameTypeDescription
valueBoolvalue

Return

TypeDescription
Booltrue if successful, false otherwise

AddChild #

Adds a class child node

method : public : AddChild(name:String) ~ JsonElement

Parameters

NameTypeDescription
nameStringclass name

Return

TypeDescription
JsonElementchild class

AppendHexEscape # function

Appends a single '\uXXXX' escape.

Int->ToHexString returns a '0x'-prefixed, unpadded value, so appending it directly produced escapes carrying the prefix, and short ones were never padded. JSON requires exactly four hex digits with no prefix.

function : AppendHexEscape(code:Int, buffer:String) ~ Nil

Parameters

NameTypeDescription
codeIntcode point to escape
bufferStringbuffer to append to

Decode # native

Deprecated: the parser decodes. Unescapes JSON escape sequences in text that still holds them, such as a string literal copied out of JSON source. Values read from a parsed document (GetValue(), GetString(), GetNames()) are ALREADY decoded, so do not pass them here: a value that holds a real backslash would be decoded a second time and corrupted (a Windows path's backslash-n would become a newline).

function : native : Decode(str:String) ~ String

Parameters

NameTypeDescription
strStringencoded string

Return

TypeDescription
Stringdecoded string

Encode # native

Escapes a string for inclusion in JSON text: the quote, the backslash and every control character below U+0020 become escapes, and every character above U+007F a u-escape (a surrogate pair outside the BMP), so the text is plain ASCII whatever carries it. The slash is written as is. An unpaired surrogate is written as the u-escape of U+FFFD, the character the parser reads one as. Serialization does this for you: ToString() and Format() escape every string value and object key on the way out, so a value handed to Insert() must be RAW text -- encoding it first would escape it twice. Call this only when building JSON text by hand.

function : native : Encode(str:String) ~ String

Parameters

NameTypeDescription
strStringdecoded string

Return

TypeDescription
Stringencoded string

FindElements #

Queries the object graph. Object attributes referenced by '/' while array elements are referenced by '[index]'. Example "cars/[last]/make".

method : public : FindElements(path:String) ~ JsonElement

Parameters

NameTypeDescription
pathStringquery path

Return

TypeDescription
JsonElementmatching element

Example

json := Data.JSON.JsonParser->TextToElement("{\"users\":[{\"name\":\"Alice\"},{\"name\":\"Bob\"}]}");
alice := json->FindElements("users/[0]/name");
alice->GetString()->PrintLine();  # Alice

Get #

Gets an indexed value from an array type

method : public : Get(index:Int) ~ JsonElement

Parameters

NameTypeDescription
indexIntindex

Return

TypeDescription
JsonElementindexed value

Example

arr := Data.JSON.JsonParser->TextToElement("[10,20,30]");
arr->Get(1)->GetInt()->PrintLine();  # 20

Get #

Gets a named value from an object type

method : public : Get(name:String) ~ JsonElement

Parameters

NameTypeDescription
nameStringelement name

Return

TypeDescription
JsonElementelement value

Example

obj := Data.JSON.JsonParser->TextToElement("{\"x\":42}");
x := obj->Get("x");   # Nil for a missing member
if(x <> Nil) {
  x->GetInt()->PrintLine();  # 42
};

GetBool #

Gets the float value

method : public : GetBool() ~ Bool

Return

TypeDescription
Boolfloat value

Example

root := Data.JSON.JsonParser->TextToElement("{\"active\":true}");
active := root->Get("active");   # Nil for a missing member
if(active <> Nil) {
  active->GetBool()->PrintLine();  # true
};

GetFloat #

Gets the float value

method : public : GetFloat() ~ Float

Return

TypeDescription
Floatfloat value

Example

root := Data.JSON.JsonParser->TextToElement("{\"pi\":3.14}");
pi := root->Get("pi");   # Nil for a missing member
if(pi <> Nil) {
  pi->GetFloat()->PrintLine();  # 3.14
};

GetInt #

Gets the integer value. A number written with a fraction or an exponent (2.5, 1e5) is truncated toward zero.

method : public : GetInt() ~ Int

Return

TypeDescription
Intinteger value

Example

root := Data.JSON.JsonParser->TextToElement("{\"port\":8080}");
port := root->Get("port");   # Nil for a missing member
if(port <> Nil) {
  port->GetInt()->PrintLine();  # 8080
};

GetNames #

Gets the names of object attributes

method : public : GetNames() ~ Vector<String>

Return

TypeDescription
Vector<String>object attribute names

GetString #

Gets the string value. A parsed string is already decoded: its escapes are the characters they stand for, so do not decode it again.

method : public : GetString() ~ String

Return

TypeDescription
Stringstring value

Example

root := Data.JSON.JsonParser->TextToElement("{\"city\":\"Oakland\"}");
city := root->Get("city");   # Nil for a missing member
if(city <> Nil) {
  city->GetString()->PrintLine();  # Oakland
};

GetType #

Gets the type

method : public : GetType() ~ JsonElement->JsonType

Return

TypeDescription
JsonElement->JsonTypetype

Example

elem := Data.JSON.JsonParser->TextToElement("42");
if(elem->GetType() = Data.JSON.JsonElement->JsonType->NUMBER) {
  "Is number"->PrintLine();
};

GetValue #

Gets the string value, decoded (see GetString)

method : public : GetValue() ~ String

Return

TypeDescription
Stringstring value

Example

elem := Data.JSON.JsonParser->TextToElement("\"hello\"");
elem->GetValue()->PrintLine();  # hello

Has #

Check to see if the indexed value exists

method : public : Has(index:Int) ~ Bool

Parameters

NameTypeDescription
indexIntindex

Return

TypeDescription
Booltrue if exists, false otherwise

Has #

Check to see if the indexed value exists

method : public : Has(name:String) ~ Bool

Parameters

NameTypeDescription
nameStringelement name

Return

TypeDescription
Booltrue if exists, false otherwise

Insert #

Insert an element

method : public : Insert(name:String, elem:JsonElement) ~ Bool

Parameters

NameTypeDescription
nameStringelement name
elemJsonElementelement

Return

TypeDescription
Booltrue if successful, false otherwise

Insert #

Inserts a value

method : public : Insert(name:String, value:String) ~ Bool

Parameters

NameTypeDescription
nameStringvalue name
valueStringvalue

Return

TypeDescription
Booltrue if successful, false otherwise

Example

obj := Data.JSON.JsonElement->New(Data.JSON.JsonElement->JsonType->OBJECT);
obj->Insert("lang", "Objeck");
obj->Insert("version", "2026");
obj->ToString()->PrintLine();

Insert #

Inserts a value

method : public : Insert(name:String, value:Int) ~ Bool

Parameters

NameTypeDescription
nameStringvalue name
valueIntvalue

Return

TypeDescription
Booltrue if successful, false otherwise

Insert #

Inserts a value

method : public : Insert(name:String, value:Float) ~ Bool

Parameters

NameTypeDescription
nameStringvalue name
valueFloatvalue

Return

TypeDescription
Booltrue if successful, false otherwise

Insert #

Inserts a value

method : public : Insert(name:String, value:Bool) ~ Bool

Parameters

NameTypeDescription
nameStringvalue name
valueBoolvalue

Return

TypeDescription
Booltrue if successful, false otherwise

IsNull #

Checks for 'null' JSON value

method : public : IsNull() ~ Bool

Return

TypeDescription
Booltrue if 'null', false otherwise

Example

elem := Data.JSON.JsonParser->TextToElement("null");
if(elem->IsNull()) {
  "Null value"->PrintLine();
};

IsNull # function

Checks for 'null' JSON value

function : IsNull(elem:JsonElement) ~ Bool

Parameters

NameTypeDescription
elemJsonElementelement to check

Return

TypeDescription
Booltrue if 'null', false otherwise

MatchType # function

Checks a node's type

function : MatchType(elem:JsonElement, type:JsonElement->JsonType) ~ Bool

Parameters

NameTypeDescription
elemJsonElementelement to check
typeJsonElement->JsonTypetype to check

Return

TypeDescription
Booltrue of matching, false otherwise

MatchValue # function

Checks a node's type

function : MatchValue(elem:JsonElement, value:String) ~ Bool

Parameters

NameTypeDescription
elemJsonElementelement to check
valueStringvalue to check

Return

TypeDescription
Booltrue of matching, false otherwise

New # constructor

Constructor

New(type:JsonElement->JsonType)

Parameters

NameTypeDescription
typeJsonElement->JsonTypeJson element type

New # constructor

Constructor

New(type:JsonElement->JsonType, value:String)

Parameters

NameTypeDescription
typeJsonElement->JsonTypeJson element type
valueStringJson string value

New # constructor

Constructor

New(value:String)

Parameters

NameTypeDescription
valueStringJson string value

New # constructor

Constructor

New(value:Int)

Parameters

NameTypeDescription
valueIntJson integer value

New # constructor

Constructor

New(value:Float)

Parameters

NameTypeDescription
valueFloatJson float value; written as the shortest text that reads back as the same value ("1.2e-05", "0.1", "3.0"), not ToString's six decimals, which wrote 1.2e-05 as 0.000012 and 1e-07 as 0.000000

New # constructor

Constructor

New(array_elems:Vector<JsonElement>)

Parameters

NameTypeDescription
array_elemsVector<JsonElement>Json array value

New # constructor

Constructor

New(map_elems:Map<String,JsonElement>)

Parameters

NameTypeDescription
map_elemsMap<String,JsonElement>Json object (names/values)

Size #

Gets the size of an array or object value

method : public : Size() ~ Int

Return

TypeDescription
Intsize of an array or object value

Example

arr := Data.JSON.JsonParser->TextToElement("[1,2,3,4,5]");
arr->Size()->PrintLine();  # 5

ToFormattedString #

Stringify and format the JSON element

method : public : ToFormattedString() ~ String

Return

TypeDescription
Stringformatted JSON string

Example

obj := Data.JSON.JsonParser->TextToElement("{\"a\":1,\"b\":2}");
obj->ToFormattedString()->PrintLine();

ToString #

Stringify the JSON element String values and object keys are escaped as they are written, so text that holds quotes, backslashes or newlines produces valid JSON; do not pre-encode values before inserting them.

method : public : ToString() ~ String

Return

TypeDescription
StringJSON string

Example

obj := Data.JSON.JsonElement->New(Data.JSON.JsonElement->JsonType->OBJECT);
obj->Insert("ok", true);
obj->ToString()->PrintLine();  # {"ok":true}