TooNetCreation
TooNetCreation

Reputation: 269

How to display multiple fields value with jq

I have this JSON :

{
  "connections": [
    {
      "resourceName": "user_1",
      "etag": "etag_value_1",
      "emailAddresses": [
        {
          "metadata": {
            "primary": true,
            "source": {
              "type": "CONTACT",
              "id": "id_1"
            }
          },
          "value": "email_1",
          "type": "home"
        }
      ]
    },
    {
      "resourceName": "user_2",
      "etag": "etag_value_2",
      "emailAddresses": [
        {
          "metadata": {
            "primary": true,
            "source": {
              "type": "CONTACT",
              "id": "id_2"
            }
          },
          "value": "email_2",
          "type": "home"
        }
      ]
    }
  ]
}

And I would like to display something like this :

{
  "resourceName": "user_1",
  "etag": "etag_value_1",
  "value": "email_1"
}

I'm playing with something like this but does not work :

jq '.connections[].emailAddresses[] | {.resourceName, .etag, .value}' test.json

How can I do that with JQ binary?

Upvotes: 1

Views: 1407

Answers (1)

Inian
Inian

Reputation: 85855

Your path expression is wrong, the fields resourceName and etag are present in the top level path inside array of connections, and the value field is inside the emailAddresses array.

Since you are forming an object, wrap the fields over {..}

.connections[] | { resourceName, etag } + ( .emailAddresses[] | { value } )

If you are looking for the first object alone, do .connections[0] or as in above for all the objects.

Upvotes: 1

Related Questions