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.

JsonParser

Support for JSON parsing

Example

doc := Web.HTTP.HttpsClient->QuickGet(Web.HTTP.Url->New("https://dummyjson.com/carts"));
if(doc <> Nil) {
   parser := Data.JSON.JsonParser->New(String->New(doc));
   if(parser->Parse()) {
      carts := parser->GetRoot()->Get("carts");   # Get is Nil for a missing member
      if(carts <> Nil & carts->Size() > 0) {
         cart := carts->Get(Int->Random(carts->Size()));
         cart_id := cart->Get("id");
         products := cart->Get("products");
         if(cart_id <> Nil & products <> Nil) {
            "cart id={$cart_id->GetInt()}\n---"->PrintLine();
            each(product := products) {
               product_id := product->Get("id");
               product_title := product->Get("title");
               if(product_id <> Nil & product_title <> Nil) {
                  "\tid={$product_id->GetInt()}, title: '{$product_title->GetString()}'"->PrintLine();
               };
            };
         };
      };
   };
};

Operations

GetError #

Get the current parsing error

method : public : GetError() ~ String

Return

TypeDescription
Stringparsing error, Nil if none

Example

parser := Data.JSON.JsonParser->New("{bad json}");
if(<>parser->Parse()) {
  parser->GetError()->PrintLine();
};

GetRoot #

Get document root

method : public : GetRoot() ~ JsonElement

Return

TypeDescription
JsonElementroot Json element

Example

parser := Data.JSON.JsonParser->New("{\"x\":1}");
if(parser->Parse()) {
  x := parser->GetRoot()->Get("x");   # Nil for a missing member
  if(x <> Nil) {
    x->GetInt()->PrintLine();  # 1
  };
};

New # constructor

Constructor

New(stream_in:String)

Parameters

NameTypeDescription
stream_inStringJSON string to process

New # constructor

Constructor

New(stream_in:Char[])

Parameters

NameTypeDescription
stream_inCharJSON character stream

New # constructor

Constructor

New(stream_in:Byte[])

Parameters

NameTypeDescription
stream_inByteJSON byte stream

Parse #

Parses a Json string

method : public : Parse() ~ Bool

Return

TypeDescription
Booltrue is successful, false otherwise

Example

parser := Data.JSON.JsonParser->New("{\"name\":\"Alice\",\"age\":30}");
if(parser->Parse()) {
  name := parser->GetRoot()->Get("name");   # Nil for a missing member
  if(name <> Nil) {
    name->GetString()->PrintLine();
  };
} else {
  parser->GetError()->PrintLine();
};

TextToElement # function

Parses JSON text

function : TextToElement(text:String) ~ JsonElement

Parameters

NameTypeDescription
textStringJSON text

Return

TypeDescription
JsonElementroot document element

Example

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