-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct way to test for the type of a JSON value #75
Comments
Thanks, i was looking for that! One hack to test for strings is:
Based on your test for "object or array", just discovered a test for objects only:
Can combine to test for arrays:
Finally, can test for number by combining "not an object or array", string test and exhaustive explicit tests for not true/false/null:
Is there a shorter way? |
While not as ingenious as @13ren's solution, you can also use the
|
On Wed, 23 Jan 2013 12:29:04 +0100, Stephen Dolan
Great! Exactly what I was looking for. I did search http://stedolan.github.com/jq/manual for such a function but This function is probably useful for others too. Regards,
|
This is great! It enables
NB: recurse omits nulls (due to its definition), and Tilo's sweet hack Would it make sense to include something like the above, as a builtin Aside:
Sorry, getting carried away here, but we can define a for-loop function, with optional step:
|
Hi,
what is the correct way to test for the type of a value?
Example: I only want to get arrays or objects and unwrap the contained values. If I don't filter out scalars jq gives an error:
% echo '[1, 2] 1' | jq '.[]'
1
2
jq: error: Cannot iterate over number
Now I test for arrays and objects "hijacking" the >= operator:
% echo '[1, 2] 1' | jq 'select(. >= []) | .[]'
1
2
Is there a better way of doing this?
And the general question is: How to test for
Regards,
Tilo
The text was updated successfully, but these errors were encountered: