Routines (alphabetical) > Routines: J > JSON_SERIALIZE

JSON_SERIALIZE

The JSON_SERIALIZE function takes a HASH, LIST, or structure variable and converts it into a JSON (JavaScript Object Notation) string.

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and for machines to parse and generate. JSON was designed as an alternative to XML, and is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. Further details can be found at http://www.json.org.

This routine is written in the IDL language. Its source code can be found in the file json_serialize.pro in the lib subdirectory of the IDL distribution.

Syntax

Result = JSON_SERIALIZE(Value)

Return Value

The result is a string containing the JSON string. If the input value is a LIST, then the JSON string will be encoded as a JSON array, with square brackets and comma-separated values. If the input value is a HASH, then the JSON string will be encoded as a JSON object, with curly braces and key-value pairs.

When converting IDL variables, the following rules are used:

Note: Since the HASH stores its key-value pairs in an arbitrary order, the key-value pairs in the resulting JSON string may be in a different order than the order in which the keys were added to the hash.

Arguments

Value

Value must be a HASH, LIST, or structure variable.

Keywords

None.

Examples

Convert a LIST of IDL values into a JSON string

mylist = LIST(1b, !NULL, 42, 3.14, "Hello")

json = JSON_SERIALIZE(mylist)

PRINT, json

IDL prints:

[true,null,42,3.14,"Hello"]

Convert a HASH of key-value pairs into a JSON string

myhash = HASH("Planet", "Jupiter", "Index", 5, "Mass", 1.9d27, "Units", "kg")

json = JSON_SERIALIZE(myhash)

PRINT, json

IDL prints:

{"Planet":"Jupiter","Index":5,"Units":"kg","Mass":1.9e27}

Convert an IDL structure into a JSON string

struct = {PLANET: "Jupiter", INDEX: 5, MASS: 1.9d27, UNITS: "kg"}

json = JSON_SERIALIZE(struct)

PRINT, json

IDL prints:

{"PLANET":"Jupiter","INDEX":5,"MASS":1.9e27,"UNITS":"kg"}

Version History

8.2

Introduced

See Also

JSON_PARSE, HASH, LIST, IDLnetURL