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"
            }
        ]
    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

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.

Community
  • 1
  • 1
peak
  • 105,803
  • 17
  • 152
  • 177