4

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

1 Answer 1

3

Try

  • jq -s '{staticstring:[{PutRequest:{Item:.[]}}]}' inputfile.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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.