Reflection/List properties: Difference between revisions

(Added Wren)
Line 333:
//[["name", "obj"], ["obj", true], ["doStuff", function()]]
</lang>
 
=={{header|jq}}==
{{works with|jq}}
 
Various properties of JSON values are directly accessible via the
built-in functions `type`, `length` (except for booleans), and, for
JSON objects, `keys` and `keys_unsorted`:
 
* `type` returns the JSON type;
* `length` returns an array's length, the number of (distinct) keys of an object, the absolute value of a number, and 0 for `null`;
 
Note that gojq (the Go implementation of jq) does not support `keys_unsorted`.
 
Other properties can be ascertained programmatically.
See e.g. '''schema.jq''' (https://gist.github.com/pkoppstein/a5abb4ebef3b0f72a6ed) for a schema-inference engine for JSON written in jq; the inferred schemas are themselves JSON documents.
 
'''Example'''
<lang jq>
# Use jq's built-ins to generate a (recursive) synopsis of .
def synopsis:
if type == "boolean" then "Type: \(type)"
elif type == "object"
then "Type: \(type) length:\(length)",
(keys_unsorted[] as $k
| "\($k): \(.[$k] | synopsis )")
else "Type: \(type) length: \(length)"
end;
 
true, null, [1,2], {"a": {"b": 3, "c": 4}, "x": "Rosetta Code"}, now
| ("\n\(.) ::", synopsis)
</lang>
{{out}}
<pre>
true ::
Type: boolean
 
null ::
Type: null length: 0
 
[1,2] ::
Type: array length: 2
 
{"a":{"b":3,"c":4},"x":[0,1]} ::
Type: object length:2
a: Type: object length:2
a: b: Type: number length: 3
a: c: Type: number length: 4
x: Type: string length: 12
 
1629526284.540229 ::
Type: number length: 1629526284.540229
</pre>
 
 
=={{header|Julia}}==
2,449

edits