Home > Back-end >  Jq extracting the name and the value of objects as an array
Jq extracting the name and the value of objects as an array

Time:12-29

I have this code and I need to get the keys and the values of the "filterFeatureGroup" object with JQ.

{
  "filterFeatureGroup": {
    "Hauttyp": [
      "Normal"
    ],
    "Deckkraft": [
      "Mittlere Deckkraft"
    ],
    "Grundfarbe": [
      "Grau"
    ],
    "Produkteigenschaften": [
      "Vegan"
    ],
    "Textur / Konsistenz / Applikation": [
      "Stift"
    ]
  }
}

My desired output is:

[
  "Hauttyp: Normal",
  "Deckkraft: Mittlere Deckkraft",
  "Grundfarbe: Grau",
  "Produkteigenschaften: Vegan",
  "Textur / Konsistenz / Applikation: Stift"
]

I have tried to put together the keys[] and values[] of the object but then I ended up with multiplying the elements in the output.

jq ".filterFeatureGroup| (keys[]| split(","))   (values[])| join(": ")"

Would anyone be able to help?

CodePudding user response:

I have tried to put together the keys[] and values[] of the object

Use to_entries for that

.filterFeatureGroup | to_entries | map("\(.key): \(.value[0])")

Online demo

  • Related