Json

Json is a value type that represents a JSON data structure.

Literal syntax

JSON structures can be written directly in Zing code. If the JSON key is a valid Zing property identifier (like name or albums below) it can be included directly, otherwise it will need to be enclosed in quotes.

Json artist = {
    name: "Rihanna",
    "birth date": "20/02/1988",
    "Active": true,
    albums: ["Good Girl Gone Bad", "Rated M"]
}

As "birth date" (contains a space) and "Active" (begins with an uppercase letter) are not valid property identifiers, they must be quoted.

Reading and writing values

Json values can be read or written directly in Zing. When a Json key is a valid Zing identifier it can be accessed using dot notation:

System.log(artist.name)
// Logs "Rihanna"
artist.realname = "Robyn Rihanna Fenty"

When a Json key is not a valid Zing identifier, subscript syntax can be used:

System.log(artist["birth date"], artist.albums[0])
// Logs "20/02/1988 Good Girl Gone Bad"
artist.albums[1] = "Rated R"

Types

Compatible types, such as Int and String, are transparently converted to their JSON equivalent on assignment.

function makeUser(String firstName, String lastName, Int height) 
    -> Json {
    
    return {
        name: {
            first: firstName,
            last: lastName
        },
        height: height,
        heightInches: height / 2.54
    }
}

static function Json.parse(String string) → Json

Parses string in JSON format, and returns the resulting Json value.

static function Json.stringify(Json json) → String

Convert the JSON value to a serialized string form, suitable for transmission or storage.

Json json = {
    firstname: "Jon",
    surname: "Snow"
}

// {"firstname":"Jon","surname":"Snow"}
System.log(Json.stringify(json))

static function Json.load(Url url) → Task<Json>

static function Json.load(Http.Request request) → Task<Json>

Load Json from a url or request.

The url can be a local file in your project, or a network URL.

static function Json.isUndefined(Json value) → Bool

static function Json.isNull(Json value) → Bool

static function Json.isBool(Json value) → Bool

static function Json.isNumber(Json value) → Bool

static function Json.isString(Json value) → Bool

static function Json.isDictionary(Json value) → Bool

static function Json.isArray(Json value) → Bool

Used to determine the type of a Json value. Returns true if the provided value is undefined, null, a boolean, a number, a string, a dictionary or an array respectively.

static function Json.length(Json value) → Int

If the value is a Json array, returns the array length. Otherwise returns 0.

static function Json.keys(Json value) → Array<String>

If value is a Json dictionary, returns an array of key names. Otherwise returns an empty array.

static function Json.array(Json value) → Array<Json>

If value is a Json array, returns an array of its values. Otherwise returns an empty array.