5

If I want to delete age from the JSON in first output. Is there a way to do that in one step, in other words, not invoking jq 2 times?

➜ $?=0 @arastogi-ld2.linkedin.biz inGraphs/common-templates [ 1:39AM] ➤ echo '[{"id": 1, "name": "Arthur", "age": "21"},{"id": 2, "name": "Richard", "age": "32"}]' | jq .
[
  {
    "id": 1,
    "name": "Arthur",
    "age": "21"
  },
  {
    "id": 2,
    "name": "Richard",
    "age": "32"
  }
]

>>>  0s elasped...
➜ $?=0 @arastogi-ld2.linkedin.biz inGraphs/common-templates [ 1:39AM] ➤ echo '[{"id": 1, "name": "Arthur", "age": "21"},{"id": 2, "name": "Richard", "age": "32"}]' | jq '.[] | del(.age)' | jq -s
[
  {
    "id": 1,
    "name": "Arthur"
  },
  {
    "id": 2,
    "name": "Richard"
  }
]

>>>  1s elasped...
➜ $?=0 @arastogi-ld2.linkedin.biz inGraphs/common-templates [ 1:39AM] ➤

1 Answer 1

10

It is quite simple when using jq using the map() call.

jq 'map(del(.age))' < json

Using map() for a given filter del(.age) will run it for each element of the input array, and return the output in a new array.

Sign up to request clarification or add additional context in comments.

Comments

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.