Home > Software design >  Prefix elements of string array with key in JQ
Prefix elements of string array with key in JQ

Time:01-11

I have a 2 jsons with its environment and the applications in it.
To compare the different (missing applications) between the 2 json,
I want to simplify the json in to an array, so that I can use minus to find the different,
but I failed to set the key as a prefix of the array.

example input:

{
  "qa": [
    "app-a",
    "app-b",
    "app-c"
  ],
  "prod": [
    "app-a",
    "app-c",
    "app-e"
  ]
}

expected output:

[
  "qa:app-a",
  "qa:app-b",
  "qa:app-c",
  "prod:app-a",
  "prod:app-c",
  "prod:app-e",
]

CodePudding user response:

Here you go:

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

Online demo

  • Related