1

I have a JSON file, with a structure like this:

{
"106" : {
      "id54011" : [
         {
            "partno1" : "16690617"
         },
         {
            "partno2" : "5899180"
         }
      ],
      "parts" : [
         "0899180",
         "16920617"
      ],
      "id5632" : [
         {
            "partno1" : "090699180"
         }
      ]
   },
   "560" : {
      "id9452" : [
         {
            "partno2" : "1569855"
         }
      ],
      "parts" : [
         "03653624",
         "15899855"
      ],
      "id578" : [
         {
            "partno3" : "0366393624"
         },
         {
            "partno4" : "0363213624"
         }
      ]
   }
}

I need to split this JSON into multiple files, using this method:

Each JSON file will consist of one object. Using the example file above, I should end up with 000106.json, and 000560.json. (All names, must have 6 digits, so zeros must be added.)

I have tried to use an iteration grouper, in python, and jq, for this, but no luck up to now.

Expected output: JSON file 1, named 000106.json:

{
   "106" : {
       "id54011" : [
           {
               "partno1" : "16690617"
           },
           {
               "partno2" : "5899180"
           }
       ],
       "parts" : [
           "0899180",
           "16920617"
       ],
       "id5632" : [
           {
               "partno1" : "090699180"
           }
       ]
   }
}

JSON file 2, named 000560.json:

{
    "560" : {
        "id9452" : [
            {
                "partno2" : "1569855"
            }
        ],
        "parts" : [
            "03653624",
            "15899855"
        ],
        "id578" : [
            {
                "partno3" : "0366393624"
            },
            {
                "partno4" : "0363213624"
            }
        ]
    }
}
2
  • What is your expected output ? Commented Mar 10, 2017 at 11:18
  • 1
    Possible duplicate of Parsing JSON with Unix tools Commented Mar 10, 2017 at 17:48

1 Answer 1

0

Since this question has both jq and awk tags, I'd recoomend using jq and awk as explained here: Split a JSON file into separate files

You can easily pad the key names in jq or awk.

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.