2

I have a json file, example.json:

[
  [
    "126",
    1522767000
  ],
  [
    "122",
    1522859400
  ],
  [
    "126",
    1523348520
  ]
]

...and would like to add multiple parent items with the desired output:

{
  "target": "Systolic",
  "datapoints": [
    [
      "126",
      1522767000
    ],
    [
      "122",
      1522859400
    ],
    [
      "126",
      1523348520
    ]
  ]
}

I'm having trouble, attempting things like:

cat example.json | jq -s '{target:.[]}', which adds the one key but not understanding how to add a value to the target and another key datapoints.

1
  • decided also to help you with some of your older queries. This one could be achieved with jtc like this: jtc -T'{"target": "Systolic", "datapoints": {{}}}' -f example.json - it will apply update right into your source example.json, so that you don't have to save/redirect the output (PS. I'm a developer of the jtc tool) Commented Sep 27, 2019 at 14:30

1 Answer 1

2

With straightforward jq expression:

jq '{target: "Systolic", datapoints: .}' example.json

The output:

{
  "target": "Systolic",
  "datapoints": [
    [
      "126",
      1522767000
    ],
    [
      "122",
      1522859400
    ],
    [
      "126",
      1523348520
    ]
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic. I had attempted to use the map function, didn't realize it was so literal. Good lesson, thank you.

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.