Using jq to parse and display multiple fields in a json serially

San picture San · Jan 27, 2015 · Viewed 200.5k times · Source

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.

Answer

Eric Hartford picture Eric Hartford · Jul 15, 2015

I recommend using String Interpolation:

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

reference