Get value if object or string if string in jq array

Viewed 218

I have a JSON object that looks like this:

[{"name":"NAME_1"},"NAME_2"]

I would like an output of

["NAME_1", "NAME_2"]

Some of the entries in the array are an object with a key "name" and some are just a string of the name. I am trying to extract an array of the names. Using

jq -cr '.[].name // []'

throws an error as it is trying to index .name of the string object. Is there a way to check if it is a string, and if so just use its value instead of .name?

4 Answers

You can use the type function which returns "object" for objects.

jq '.[] | if type == "object" then .name else . end' file.json

To get the output as array, just wrap the whole expression into [ ... ].

Just use the error suppression operator with ?, map and scalars

jq 'map( .name?, scalars )'

Note that by using scalars, it is assumed that other than objects with name, all others are names of form NAME_*. If there are other strings as well, and you need to exclude some of them you might need to add some additional logic to do that. e.g. using startswith(..) with a string of your choice.

map( .name?, select( scalars | startswith("NAME") ) )

Demo

With your shown samples only, please try following jq code. Using tostream function here to get the required values from requirement.

jq -c '[.[] | tostream | if .[1] != null then .[1] else empty end]' Input_file
Related