1

I have a json fragment with an array of keys and a separate array of values. Key 1 should match up to Value 1, etc. I'm trying to reformat with jq but not having much luck.

Original JSON:

{
  "result": {
    "event.KeyValues{}.Key": [
      "name",
      "gender",
      "employee",
      "email"
    ],
    "event.KeyValues{}.Value": [
      "tyler",
      "male",
      "yes",
      "[email protected]"
    ],
    "foo": "1",
    "bar": "2"
  }
}

Desired Output:

{
    "name": "tyler",
    "gender": "male",
    "employee": "yes",
    "email": "[email protected]"
}

1 Answer 1

2

Use transpose to pair keys and values. Then you can make an object out of each pair and add them together to get the desired structure.

.result
| [."event.KeyValues{}.Key", ."event.KeyValues{}.Value"]
| transpose
| map({(.[0]): .[1]})
| add

Online demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.