Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check JSON data type using jq

Tags:

json

types

bash

jq

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?

like image 596
cameraguy258 Avatar asked Jan 15 '19 00:01

cameraguy258


People also ask

Can jq validate JSON?

jq – a lightweight and flexible CLI processor – can be used as a standalone tool to parse and validate JSON data.

How extract JSON data from jq?

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.

Does jq use JSONPath?

jp is a JSON processor for the command line using JSONPath (aka "a simpler jq, and with JSONPath").

What is jq format?

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.


1 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
like image 64
hek2mgl Avatar answered Nov 08 '22 15:11

hek2mgl