I'm trying to check the actual data type of a JSON value belonging to a specific key.
test.json
{
"id": 50,
"name": "Joe"
}
I want something analogous to this:
$ `jq 'typeof("id")' < test.json`
(output)>> string
Is this possible using jq
?
jq – a lightweight and flexible CLI processor – can be used as a standalone tool to parse and validate JSON data.
The simplest way to extract data from a JSON file is to provide a key name to obtain its data value. Type a period and the key name without a space between them. This creates a filter from the key name. We also need to tell jq which JSON file to use.
jp is a JSON processor for the command line using JSONPath (aka "a simpler jq, and with JSONPath").
jq is a free open source JSON processor that is flexible and straightforward to use. It allows users to display a JSON file using standard formatting, or to retrieve certain records or attribute-value pairs from it.
Use type
:
jq -r '[1.23,"abc",true,[],{},null][]| type' <<< '""'
number
string
boolean
array
object
null
In your example you could check:
jq '.id|type=="number"' file.json
Or use it in a select
filter to display those ids which are not numbers for example:
jq '.[]|select(id|type=="number"|not)' file.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With