JSON

JSON parsing and generation for data interchange. The Format module provides JSONObject and JSONArray classes for working with JSON data.

xxml
#import Language::Core;
#import Language::Format;

JSONObject

Key-value object for JSON data.

Constructor

MethodParametersReturnsDescription
ConstructorJSONObject^Create empty JSON object

Object Operations

MethodParametersReturnsDescription
setkey: String^, value: String^NoneSet key to value
getkey: String^String^Get value or empty string
haskey: String^Bool^Check if key exists
removekey: String^NoneRemove key
sizeInteger^Number of pairs
xxml
Instantiate Format::JSONObject^ As <obj> = Format::JSONObject::Constructor();
Run obj.set(String::Constructor("name"), String::Constructor("Alice"));
Run obj.set(String::Constructor("city"), String::Constructor("Seattle"));

If (obj.has(String::Constructor("name"))) -> {
    Instantiate String^ As <name> = obj.get(String::Constructor("name"));
    Run Console::printLine(name);  // "Alice"
}

Type-Specific Getters

MethodParametersReturnsDescription
getIntegerkey: String^Integer^Parse value as integer
getBoolkey: String^Bool^Parse value as boolean
xxml
Run obj.set(String::Constructor("age"), String::Constructor("25"));
Run obj.set(String::Constructor("active"), String::Constructor("true"));

Instantiate Integer^ As <age> = obj.getInteger(String::Constructor("age"));
Instantiate Bool^ As <active> = obj.getBool(String::Constructor("active"));

Serialization

MethodParametersReturnsDescription
stringifyString^Convert to JSON string
xxml
Instantiate String^ As <json> = obj.stringify();
Run Console::printLine(json);  // {"name":"Alice","age":"25"}

JSONArray

Ordered array of JSON values.

Constructor

MethodParametersReturnsDescription
ConstructorJSONArray^Create empty JSON array

Array Operations

MethodParametersReturnsDescription
addvalue: String^NoneAdd to end
getindex: Integer^String^Get value at index
removeindex: Integer^NoneRemove at index
sizeInteger^Number of elements
xxml
Instantiate Format::JSONArray^ As <arr> = Format::JSONArray::Constructor();
Run arr.add(String::Constructor("first"));
Run arr.add(String::Constructor("second"));

Instantiate String^ As <first> = arr.get(Integer::Constructor(0));
Run Console::printLine(first);  // "first"

Serialization

MethodParametersReturnsDescription
stringifyString^Convert to JSON string
xxml
Instantiate String^ As <json> = arr.stringify();
Run Console::printLine(json);  // ["first","second"]

Note

All values in JSONObject and JSONArray are stored as strings. Use type-specific getters (getInteger, getBool) for automatic parsing.

Complete Example

json_example.xxml
1#import Language::Core;
2#import Language::Format;
3#import Language::System;
4
5[ Entrypoint
6 {
7 // Build a JSON object
8 Instantiate Format::JSONObject^ As <person> = Format::JSONObject::Constructor();
9 Run person.set(String::Constructor("name"), String::Constructor("Alice"));
10 Run person.set(String::Constructor("age"), String::Constructor("30"));
11 Run person.set(String::Constructor("active"), String::Constructor("true"));
12
13 // Output as JSON
14 Run Console::printLine(String::Constructor("Person JSON:"));
15 Run Console::printLine(person.stringify());
16
17 // Read values back
18 Instantiate String^ As <name> = person.get(String::Constructor("name"));
19 Instantiate Integer^ As <age> = person.getInteger(String::Constructor("age"));
20 Instantiate Bool^ As <active> = person.getBool(String::Constructor("active"));
21
22 Run Console::printLine(String::Constructor("Name: ").append(name));
23 Run Console::printLine(String::Constructor("Age: ").append(age.toString()));
24
25 // Build a JSON array
26 Instantiate Format::JSONArray^ As <items> = Format::JSONArray::Constructor();
27 Run items.add(String::Constructor("apple"));
28 Run items.add(String::Constructor("banana"));
29 Run items.add(String::Constructor("cherry"));
30
31 Run Console::printLine(String::Constructor("Items JSON:"));
32 Run Console::printLine(items.stringify());
33
34 Exit(0);
35 }
36]

See Also