The JSON_PARSE function takes a JSON (JavaScript Object Notation) string (or file) and converts it into an IDL variable.
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_parse.pro in the lib subdirectory of the IDL distribution.
Result = JSON_PARSE(String [, /TOARRAY] [, /TOSTRUCT])
If String begins with a curly brace "{", then the result is a HASH (or a structure if TOSTRUCT is set), containing the unordered key-value pairs from the JSON string. If String begins with a square bracket "[", then the result is a LIST (or an array if TOARRAY is set), containing the ordered collection of values.
If String is a scalar string that does not begin with "{" or "[", then it is assumed to be a file name. If the file exists, then the contents of the file are read and parsed.
When converting JSON values into IDL variables, the following rules are used:
Note: Since the HASH stores its name-value pairs in an arbitrary order, the HASH::Keys() method may return the name-value pairs in a different order compared to the original JSON string.
String must be a valid JSON string containing either a JSON object of name-value pairs, or a JSON array of values.
If this keyword is set, then any JSON arrays within the result (including the result itself) will be returned as IDL arrays, instead of LIST variables.
Note: For this keyword to work correctly, each JSON array within the result must contain values that are all of the same type. If you are unsure of the data types, you should not use this keyword and return the result as a LIST instead.
If this keyword is set, then any JSON objects within the result (including the result itself) will be returned as IDL structures, instead of HASH variables.
Note: Since IDL structure tags must be valid IDL identifiers, any spaces or special characters within the JSON keys will be converted to underscore characters. In addition, all keys will be converted to uppercase. Finally, any "null" values will be converted to the string "!NULL" since you cannot have a null field in a structure. If you need to convert the result back into an equivalent JSON string, you should not use this keyword and return the result as a HASH instead.
json = '[true,null,42,3.14,"Hello"]'
result = JSON_PARSE(json)
HELP, result
IDL prints:
RESULT LIST <ID=4 NELEMENTS=5>
json = '{"Planet":"Jupiter","Index":5,"Units":"kg","Mass":1.9e27}'
result = JSON_PARSE(json)
HELP, result
PRINT, result.Keys()
IDL prints:
RESULT HASH <ID=22 NELEMENTS=4>
Planet
Index
Units
Mass
json = '["Mercury", "Venus", "Earth"]'
result = JSON_PARSE(json, /TOARRAY)
PRINT, result
IDL prints:
Mercury
Venus
Earth
mystruct = {TRUE: 1B, FALSE: 0B, INT: 5, FLOAT: 3.14, STR: "Hello", $
INTARRAY: [1,2,3,4,5], FLOATARRAY: [1.0,2.0,3.0]}
json = JSON_SERIALIZE(mystruct)
output = JSON_PARSE(json, /TOARRAY, /TOSTRUCT)
HELP, output
IDL prints:
** Structure <1d8ea18>, 7 tags, length=104, data length=94, refs=1:
TRUE BYTE 1
FALSE BYTE 0
INT LONG64 5
FLOAT DOUBLE 3.1400001
STR STRING 'Hello'
INTARRAY LONG64 Array[5]
FLOATARRAY DOUBLE Array[3]
8.2 |
Introduced |