Skip to content
Advertisement

How to jq to copy array inside the dictionary?

I have the following json

   [ 
       { 
          "name":"bucket1"
       },
       { 
          "name":"bucket1"
       }
    ]

I want to convert it to

{ 
   "buckets":[ 
      { 
         "name":"bucket1"
      },
      { 
         "name":"bucket1"
      }
   ]
}

How do I do this with jq?

Advertisement

Answer

The solution is the following:

{"buckets":.}

In the Command line:

jq '{"buckets":.}'

See:https://jqplay.org/s/MJ-GoLOBX2


There is more documentation available at:

https://programminghistorian.org/en/lessons/json-and-jq#the-dot-

Also see the hints below jqplay at the bottom.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement