parse. Parsing JSON string into hash

^json:parse[JSON-string;parsing options]

Method transforms JSON-string into a hash.

Parsing options-hash with parsing options.

Default
Value
$.depth(number)
19
Maximum depth
$.double(true|false)
true
true   -Stores real numbers as objects of class double.
false   -Stores real numbers as strings.
$.int(true|false)
true
true   -Stores integer numbers as objects of class int.
false   -Stores integer numbers as strings.   [3.4.4]
$.distinct[first|last|all]
not defined
The way of processing elements with identical names:
first   -Only the first element will be stored in output.
last   -Only the last element will be stored in output.
all   -All elements will be stored in output. Elements starting from the second will have suffixes _2, _3, etc.
default -elements with identical names are considered error (exception).
$.object[method name]
not defined
If defined the specified method will be called for every parsing object. The result of the method will be passed to output. The method must accept two parameters-name and value.
$.array[method name]
not defined
If defined the specified method will be called for every parsing array. The result of the method will be passed to output. The method must accept two parameters-name and value.   [3.4.2]
$.taint[transformation type]
not defined
Defines the transformation type for all strings in the output.   [3.4.2]


If the json came from an external source, it is necessary to designate trust in data,
for example using
^taint[clean;$form:json].

Example
@main[]
$json_string[
{
   "a1":{"b": 1, "c": "
abc", "d": "xyz"},
   "a2":{
"b": 1.1, "b": 2.2, "b": 3.3, "d": {"da": 11, "db": 22}}
}
]

$h[^json:parse[$json_string;
   $.double(false)

   $.distinct[all]
   $.object[$
object_handler]
]]

@
object_handler[key;value]
$result[^if($key eq "d"){object with key='$key' and ^eval($value) fields}{$value}]

The specified JSON-string will be transformed into the following hash:

$h[
   $.a1[
      $.b
(1)
      $.c[
abc]
      $.d[xyz]
   ]
   $.a2[
      $.b
[1.1]
      $.b_2
[2.2]
      $.b_3
[3.3]
      $.d[object with key='d' and 2 fields]

   ]
]




Copyright © 1997–2017 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 12.04.2017