Mornor
Mornor

Reputation: 3783

Transforming a list containing key/value strings with jq

So, I basically have a file test.json

[
  "Name=TestName",
  "Tag=TestTag"
]

Which I'd like to transform into

[
  {
    "ParameterKey": "Name",
    "ParameterValue": "TestName",
  },
  {
    "ParameterKey": "Tag",
    "ParameterValue": "TestTag",
  }
]

With jq. Any idea?

Upvotes: 0

Views: 219

Answers (2)

Inian
Inian

Reputation: 85560

You don't need to use split() call twice but just once and access the results directly with the Array/Object Value Iterator: .[] and specifying the index inside

jq -n '[ inputs[] | split("=") | {ParameterKey: .[0], ParameterValue: .[1]} ]'

Upvotes: 3

Amit Badheka
Amit Badheka

Reputation: 2672

You can try JQ Play

I tried with the following jq. It should work as long as you are sure of the format of the array.

[.[] | {ParameterKey: split("=")[0], ParameterValue: split("=")[1]}]

If you are using from terminal, you can use the following option

cat test.json | jq '[.[] | {ParameterKey: split("=")[0], ParameterValue: split("=")[1]}]'

Upvotes: 2

Related Questions