All help topics · Link to this topic
Showing help on 'parse_json()'
Syntax: parse_json (STR json [, STR mode])Returns the MOO value representation of the JSON string. If the specified string is not valid JSON, E_INVARG is raised.
The optional mode specifies how this function handles conversion of MOO values into their JSON representation. The options are the same as for generate_json().
parse_json("{}") => []
parse_json("{\"foo\":\"bar\"}") => ["foo" -> "bar"]
parse_json("{\"foo\":\"bar\"}", "common-subset") => ["foo" -> "bar"]
parse_json("{\"foo\":\"bar\"}", "embedded-types") => ["foo" -> "bar"]
parse_json("{\"foo\":1.1}") => ["foo" -> 1.1]
parse_json("{\"foo\":1.1}", "common-subset") => ["foo" -> 1.1]
parse_json("{\"foo\":1.1}", "embedded-types") => ["foo" -> 1.1]
parse_json("{\"foo\":\"#1\"}") => ["foo" -> "#1"]
parse_json("{\"foo\":\"#1\"}", "common-subset") => ["foo" -> "#1"]
parse_json("{\"foo\":\"#1|obj\"}", "embedded-types") => ["foo" -> #1]
parse_json("{\"foo\":\"E_PERM\"}") => ["foo" -> "E_PERM"]
parse_json("{\"foo\":\"E_PERM\"}", "common-subset") => ["foo" -> "E_PERM"]
parse_json("{\"foo\":\"E_PERM|err\"}", "embedded-types") => ["foo" -> E_PERM]
parse_json("{\"foo\":true,\"bar\":false,\"baz\":null}") => ["bar" -> false, "baz" -> E_NONE, "foo" -> true]
In embedded types mode, key values can be converted to MOO types by appending type information. The full set of supported types are obj, str, err, float and int.
parse_json("{\"1\":2}") => ["1" -> 2]
parse_json("{\"1\":2}", "common-subset") => ["1" -> 2]
parse_json("{\"1|int\":2}", "embedded-types") => [1 -> 2]
parse_json("{\"#1|obj\":2}", "embedded-types") => [#1 -> 2]
JSON boolean values are converted to MOO BOOL values. JSON null values are converted to E_NONE.