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 numbers as objects of class double.
false   - Stores numbers as strings.
$.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 element. The result of the method will be passed to output. The method must accept two parameters - name and value.


Example
@main[]
$json_string[
{
   "a1":{"b": 1, "c": "xyz", "d": "zzz"}
,
   "a2":{
"b": 1, "b": 2, "b": 3}
}
]

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

   $.distinct[all]
   $.object[$handler]
]]

@handler[key;value]
$result[^if($keq eq "d"){=^value.length[]=}{$value}]

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

$h[
   $.a1[
      $.b[1]
      $.c[xyz]
      $.d[=3=]
   ]
   $.a2[
      $.b[1]
      $.b_2[2]
      $.b_3[3]
   ]
]





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