463

I have this Json

{
    "users": [
        {
            "first": "Stevie",
            "last": "Wonder"
        },
        {
            "first": "Michael",
            "last": "Jackson"
        }
    ]
}

Using jq I'd like to display first and last name serially. Like so -

Stevie Wonder
Michael Jackson

This is how far I have gotten -

jq '.users[].first, .users[].last'

But it displays

"Stevie"
"Michael"
"Wonder"
"Jackson"

Notice the following:

  1. The double quotes that I do not want.
  2. The carriage return that I do not want.
  3. It's jumbled up. My query displays all the first names first, and then all the last names. However, I want first-last, first-last pair.
syntagma
  • 23,346
  • 16
  • 78
  • 134
San
  • 5,051
  • 3
  • 16
  • 12

8 Answers8

635

I recommend using String Interpolation:

jq '.users[] | "\(.first) \(.last)"'

We are piping down the result of .users[] to generate the string ".first .last" using string interpolation. \(foo) syntax is used for string interpolation in jq. So, for the above example, it becomes "Stevie Wonder" (".users[].first .users[].second" working elementwise) and "Michael Jackson".

jq reference: String interpolation

Amit Singh
  • 2,875
  • 14
  • 30
Eric Hartford
  • 16,464
  • 4
  • 33
  • 50
362

You can use addition to concatenate strings.

Strings are added by being joined into a larger string.

jq '.users[] | .first + " " + .last'

The above works when both first and last are string. If you are extracting different datatypes(number and string), then we need to convert to equivalent types. Referring to solution on this question. For example.

jq '.users[] | .first + " " + (.number|tostring)'
divinedragon
  • 5,105
  • 13
  • 50
  • 97
abraham
  • 46,583
  • 10
  • 100
  • 152
  • 62
    To eliminate the JSON quotation marks, invoke jq with the -r option, e.g. jq -r '.users[] | .first + " " + .last' – peak Sep 07 '15 at 04:41
  • 4
    +1, but for my use case, I'm trying to format two numbers onto the same row. This approach fails because it can't add `" "` to a number. Eric's answer gives a better result for this case. – Synesso Nov 26 '15 at 21:55
  • 9
    @Synesso: `(.numA|tostring) + " " + (.numB|tostring)` should work. Or use string interpolation instead: `"\(.numA) \(.numB)"`. – Mr. Lance E Sloan Aug 03 '17 at 14:45
  • When I did `jq '.users[] | .first + " " + .last'`, it worked very well, but caused a newline between the value of `.first` and `.last`. I changed the `" "` to `"@"` and then did a `sed 's/@/ /g'` on the output to get "John Smith" as the output. Something like this: `jq '.users[] | .first + "@" + .last' | sed 's/@/ /g'` – Kartik Pandya Jul 02 '19 at 17:07
75
jq '.users[]|.first,.last' | paste - -
optman
  • 851
  • 6
  • 3
60

In addition to what others have suggested, I think that two options are worth mentioning.

Print as CSV/TSV

$ cat file.json | jq -r '.users[] | [.first, .last] | @tsv'
Stevie  Wonder
Michael Jackson
cat file.json | jq -r '.users[] | [.first, .last] | @csv'
"Stevie","Wonder"
"Michael","Jackson"

The first expression, .users[], unnests the objects from the outer-most array as in the code given in the question. The next expression, [.first, .last], creates a new array of the values for each input object, and the final expression uses the built-in functions @tsv and @csv to print all input arrays as tab-separated and comma-seperated values, respectively.

Print as JSON values

Similarly, it is possible to construct JSON values again, which is interesting if you just want to keep a subset of the fields:

$ cat file.json | jq -c '.users[] | {first}'
{"first":"Stevie"}
{"first":"Michael"}
ingomueller.net
  • 4,097
  • 2
  • 36
  • 33
19

my approach will be (your json example is not well formed.. guess thats only a sample)

jq '.Front[] | [.Name,.Out,.In,.Groups] | join("|")'  front.json  > output.txt

returns something like this

"new.domain.com-80|8.8.8.8|192.168.2.2:80|192.168.3.29:80 192.168.3.30:80"
"new.domain.com -443|8.8.8.8|192.168.2.2:443|192.168.3.29:443 192.168.3.30:443"

and grep the output with regular expression.

Ganesh Chandrasekaran
  • 1,578
  • 12
  • 17
  • I wonder what this has to do with the question...? Also — why do you claim that the JSON example given by the OP is 'not well formed'? It looks great to me — and to syntax-checking tools, like, aye, `jq` itself. Also: can you show the contents of `front.json? – Gwyneth Llewelyn Dec 15 '22 at 12:27
  • The comment was made in 2020, and someone edited the question in 2021. What you are seeing is the latest version of the question. – Ganesh Chandrasekaran Dec 20 '22 at 21:07
18

This will produce an array of names

> jq '[ .users[] | (.first + " " + .last) ]' ~/test.json

[
  "Stevie Wonder",
  "Michael Jackson"
]
TinyRoy
  • 309
  • 2
  • 4
17

While both of the above answers work well if key,value are strings, I had a situation to append a string and integer (jq errors using the above expressions)

Requirement: To construct a url out below json

pradeep@seleniumframework>curl http://192.168.99.103:8500/v1/catalog/service/apache-443 | jq .[0]
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   251  100   251    0     0   155k      0 --:--:-- --:--:-- --:--:--  245k
{
  "Node": "myconsul",
  "Address": "192.168.99.103",
  "ServiceID": "4ce41e90ede4:compassionate_wozniak:443",
  "ServiceName": "apache-443",
  "ServiceTags": [],
  "ServiceAddress": "",
  "ServicePort": 1443,
  "ServiceEnableTagOverride": false,
  "CreateIndex": 45,
  "ModifyIndex": 45
}

Solution:

curl http://192.168.99.103:8500/v1/catalog/service/apache-443 |
jq '.[0] | "http://" + .Address + ":" + "\(.ServicePort)"'
anubhava
  • 761,203
  • 64
  • 569
  • 643
machzqcq
  • 1,029
  • 13
  • 15
  • note that escaping the closing parenthesis is not needed, and would err. – nymo Jun 30 '17 at 02:08
  • 2
    @nymo: That's not escaping. `\(...)` is string interpolation. Here it turns numeric `.ServicePort` into string. Interpolation could be used in place of the `+` signs to make this solution shorter. – Mr. Lance E Sloan Aug 03 '17 at 14:41
6

I got pretty close to what I wanted by doing something like this

cat my.json | jq '.my.prefix[] | .primary_key + ":", (.sub.prefix[] | "    - " + .sub_key)' | tr -d '"' 

The output of which is close enough to yaml for me to usually import it into other tools without much problem. (I am still looking for a way to basicallt export a subset of the input json)

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147