1

I have an object which has an array of objects. I am willing to append two properties of each object inside the array and create a new key out of that. I am new to JQ and have tried various ways to do this, but not able to figure out. Need help.

Input:

{
  "name": "Toyota",
  "Model": "Innova",
  "Details": [
    {
      "entry_day": "23",
      "entry_month": "May"
    },
    {
      "entry_day": "01",
      "entry_month": "Jan"
    }
  ]
}

Output I expect:

{
    "name": "Toyota",
    "Model": "Innova",  
    "Details": [
        {
            "entry_date": "23 May"
        },
        {
            "entry_date": "01 Jan"
        }
    ]
}
1
  • your json is invalid: Jan and May should be in double quotes Commented Jun 1, 2017 at 9:11

1 Answer 1

5

You need to use the update assignment operator |=:

jq '(.Details[]|={entry_date:"\(.entry_day) \(.entry_month)"})' input.json
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.