0

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?

Inian
  • 80,270
  • 14
  • 142
  • 161
Mornor
  • 3,471
  • 8
  • 31
  • 69
  • I tried a lot, trust me. `cat test.json | jq .[] | [.ParameterKey, .ParameterValue]` .... – Mornor May 12 '20 at 12:25
  • 1
    Questions seeking code help must include the shortest code necessary to reproduce it in the question itself preferably in a [Stack Snippet](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/). See How to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). It also very helpful to show in your Question an expected result, and quote any (exact) errors you are getting. – Ron May 12 '20 at 12:33

2 Answers2

3

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]} ]'
Inian
  • 80,270
  • 14
  • 142
  • 161
2

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]}]'

Amit Badheka
  • 2,677
  • 4
  • 19
  • 29