Type detection: Difference between revisions
Content added Content deleted
Line 997: | Line 997: | ||
console.log(typeof(12345)); // Returns number |
console.log(typeof(12345)); // Returns number |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
In jq, the function that returns the JSON type of a JSON entity is |
|||
<code>type/0</code>. It returns "object", "array", "boolean", "string", or "number". |
|||
Given arbitrary UTF-8 input, it could be used like so: |
|||
try type catch "invalid JSON" |
|||
Given some text of unknown type, a "typeof" function could be |
|||
written to determine whether the string could be interpreted as a |
|||
JSON document, and if so, what JSON type it would have, as follows: |
|||
def typeof: |
|||
try (fromjson | type) catch "string" ; |
|||
Here is an illustrative transcript from an interactive session showing input and output on alternate lines: |
|||
<pre> |
|||
$ jq -R 'try (fromjson | type) catch "string"' |
|||
abc |
|||
"string" |
|||
{"a":1,"b":2} |
|||
"object" |
|||
[1,"a"] |
|||
"array" |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |