When given an Object with two sibling properties, using jq how do I create a new Object where the sibling property is moved to be a child property?

Viewed 64

I have an array of Objects in the following form:

{
    "alpha": {
        "beta": "gamma",
        "delta": "epsilon"
    },
    "zeta": {
        "eta": "theta",
        "iota": "kappa"
    }
}

Using jq, I would like to transform this Object to be in the following form:

{
    "alpha": {
        "beta": "gamma",
        "delta": "epsilon",
        "zeta": {
            "eta": "theta",
            "iota": "kappa"
        }
    },
}

How do I do that? I'm not especially familiar with jq's syntax and the manual isn't presenting any clear answers, at least as far as I can see.

Thanks in advance!

2 Answers
$ jq '{alpha: (.alpha + {zeta})}' tmp.json
{
  "alpha": {
    "beta": "gamma",
    "delta": "epsilon",
    "zeta": {
      "eta": "theta",
      "iota": "kappa"
    }
  }
}

{zeta} is shorthand for {zeta: .zeta}.

Use assignment to add the zeta field where you want it, and del to remove it where you don't want it anymore.

jq '.alpha.zeta=.zeta|del(.zeta)' input.json
Related