skip to Main Content

I have the following JSON object:

{
...,
"projects": [
    "abc",
    "xyz"
  ],
...
}

And I want it transformed to:

{
  "abc": {
    "name": "abc"
  },
  "xyz": {
    "name": "xyz"
  },
}

I’m having trouble creating this. I’ve tried using map as .projects | map({(.): { "name": (.) }} ) but it’s not in the format I want and ends up in an array.

2

Answers


  1. You’re looking for something like this:

    .projects | map({(.): {name: .}}) | add
    

    Online demo

    Login or Signup to reply.
  2. I have a bit of a functional mindset, so I’d reach for reduce here:

    reduce .projects[] as $name ({}; . + {($name): {name: $name}})
    

    outputs

    {
      "abc": {
        "name": "abc"
      },
      "xyz": {
        "name": "xyz"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search