1

I want to re-organize a json using jq. My json is like:

[
  {
    "id": "id1",
    "name": "Robin"
  },
  {
    "id": "id2",
    "name": "John"
  },
  {
    "id": "id3",
    "name": "Jane"
  }
]

I want to have an output as :

[
  {
    "Robin": "id1"
  },
  {
    "John": "id2"
  },
  {
    "Jane": "id3"
  }
]

How to achieve this?

Thanks in advance

Sayan
  • 65
  • 6

2 Answers2

0

Yu need:

cat data1.jtxt | jq 'to_entries|map({(.value.name):(.value.id)})'

My output:

[
      {
        "Robin": "id1"
      },
      {
        "John": "id2"
      },
      {
        "Jane": "id3"
      }
    ]
0

I cannot understand how was this command so difficult to find

[.[] | {(.id):.name}] | add

gives you:

{
  "id1": "Robin",
  "id2": "John",
  "id3": "Jane"
}

If you want to keep the "object separation" just remove the | add

This question somehow led me to it.

vladimirror
  • 729
  • 12
  • 8