Check JSON data type using jq

Viewed 7656

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?

2 Answers

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
Related