JSON

JSON all things!

BoxLang supports native JSON support via several key functions and some member functions.

Serialize

BoxLang gives us the jsonSerialize() function to convert any piece of data to its JSON representation (https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/jsonserialize)

jsonSerialize(
 var
 [, serializeQueryByColumns = false ]
)

Pass in any complex or simple variable to the var argument and JSON will be produced:

person = { name = "Luis Majano", company = "Ortus Solutions", year = 2006};
writeOutput( jsonSerialize( person ) );

You can even use the toJSON() member function:

person = { name = "Luis Majano", company = "Ortus Solutions", year = 2006};
writeOutput( person.toJSON() );

Key Casing

By default BoxLang will keep the keys in a struct in their original casing in the resulting JSON document:

person = { name = "Luis Majano", company = "Ortus Solutions", year = 2006};
writeOutput( jsonSerialize( person ) );

// Will become
{ "name" : "Luis Majano", "company" : "Ortus Solutions", "year" : 2006 }

Pretty JSON

You can leverage the pretty argument to jsonSerialize() function to enable formatted JSON output for improved readability.

Example:

data = {
    name: "John Doe",
    age: 30,
    address: {
        street: "123 Main St",
        city: "Anytown",
        country: "USA"
    },
    hobbies: ["reading", "cycling", "photography"]
};

// Standard compact JSON (default behavior)
compactJson = jsonSerialize( data );
// Output: {"name":"John Doe","age":30,"address":{"street":"123 Main St","city":"Anytown","country":"USA"},"hobbies":["reading","cycling","photography"]}

// Pretty formatted JSON (new feature)
prettyJson = jsonSerialize( data: data, pretty: true );
/* Output:
{
  "name" : "John Doe",
  "age" : 30,
  "address" : {
    "street" : "123 Main St",
    "city" : "Anytown",
    "country" : "USA"
  },
  "hobbies" : [ "reading", "cycling", "photography" ]
}
*/

// Useful for debugging and configuration files
writeFile( "config.json", jsonSerialize( data: appConfig, pretty: true ) );

Deserialize

The inverse of serialization is deserialization (https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/jsondeserialize). BoxLang gives you the jsonDeserialize() function that will take a JSON document and produce native BoxLang data structures for you.

jsonDeserialize(
 json
 [, strictMapping = true ]
)

Just pass a JSON document, and off we go with native structs/arrays/dates/strings and booleans.

if( isJson( mydata ) ){
    return jsonDeserialize( data );
}

person = jsonDeserialize( '{"company":"Ortus","name":"Mr OrtusMan"}' );
writeOutput( person.company );

This function can also be used as a member function in any string literal:

var deserializedData = myjsonString.jsonDeserialize();
var data = '[]'.jsonDeserialize();

Is this JSON?

BoxLang has a function to test if the incoming string is valid JSON (https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/decision/isjson) or not: isJSON()

isJSON( "[ 1, 2, 3 ]" )

Last updated

Was this helpful?