bsj4sla
bsj4sla

Reputation: 73

adding parent element to json objects using jq/bash

I want to add a parent to each of the json objects within a file. My starting point is the following json file containing two json items:

{
  "id": {
    "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63"
  },
  "test": {
    "N": "5"
  },
  "added": {
    "S": "2017-02-15T17:56:19.958917+00:00"
  },
  "foo": {
    "N": "88"
  },
  "web": {
    "N": "103"
  }
}
{
  "id": {
    "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63"
  },
  "image_server_id": {
    "N": "5"
  },
  "added": {
    "S": "2017-02-15T17:56:19.958917+00:00"
  },
  "result": {
    "N": "88"
  },
  "data": {
     "foo": {
       "N": "103",
       "S": "test"
     }
  }
}

Using jq and/or bash I want to generate the following json file:

{
   "*StaticString*": [
     {
        "PutRequest": {
            "Item": {
               "id": {
                 "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63"
               },
               "test": {
                 "N": "5"
               },
               "added": {
                 "S": "2017-02-15T17:56:19.958917+00:00"
               },
               "foo": {
                 "N": "88"
               },
               "web": {
                 "N": "103"
               }   
            **}
         }
     },
     {
        "PutRequest": {
            "Item": {
               "id": {
                 "S": "cf7ebec368f241ead7ecf818ce9ed098406afa63"
               },
               "image_server_id": {
                 "N": "5"
               },
               "added": {
                 "S": "2017-02-15T17:56:19.958917+00:00"
               },
               "result": {
                 "N": "88"
               },
               "data": {
                 "foo": {
                   "N": "103",
                   "S": "test"
                 }
               }  
            **}
        }
     }
   ]
  }

To sum up I want to add

{
"StaticString": [
{

at the beginning of the file. Then I need to put each json item into a parent

   "PutRequest": {
            "Item": {
            ...
            }
   }

and generate an array out of the json items.

I'm already knowing how to generate an array of the json items using jq -s . testfile.json But I don't know how to add a parent to each json item.

I hope it's clear what I want to achieve.

Thank's for your help, Chris

Upvotes: 4

Views: 1911

Answers (1)

hmedia1
hmedia1

Reputation: 6200

Try

  • jq -s '{staticstring:[{PutRequest:{Item:.[]}}]}' inputfile.json

Upvotes: 3

Related Questions