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
static async function Json.parseAsync(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 async function Json.load(Url url) → Json
static async function Json.load(Http.Request request) → Json
Load Json from a url or request.
The url can be a local file in your project, or a network URL.
Returns the underlying type of the Json value.
The possible types are:
Json.Type.Undefined
Json.Type.Null
Json.Type.Bool
Json.Type.Number
Json.Type.String
Json.Type.Dictionary
Json.Type.Array
function isDictionary() → Bool
Returns true if the Json value is of the corresponding type, otherwise false.
function toDictionary() → Dictionary<String, Json>
function toArray() → Array<Json>
Convert the Json value into the corresponding native Zing type.
function keys() → Array<String>
If value is a Json dictionary, returns an array of key names. Otherwise returns an empty array.