Home > Net >  Python read a JSON file of objects and make multiple json files from each object [closed]
Python read a JSON file of objects and make multiple json files from each object [closed]

Time:09-17

So I have a JSON file with multiple objects like this:

{
  "0": {
   "name": "DIGI#0",
   "description": "DIGI",
   "image": "//asset/0",
   "attributes": [
    {
     "trait_type": "background",
     "value": "pink"
    },
    {
     "trait_type": "fur",
     "value": "light grey"
    },
    {
     "trait_type": "Mouth",
     "value": "yawn"
    },
    {
     "trait_type": "clothes",
     "value": "suit"
    },
    {
     "trait_type": "accessories",
     "value": "hat"
    }
   ]
  },
  "1": {
   "name": "DIGI#1",
   "description": "DIGI",
   "image": "//asset/1",
   "attributes": [
    {
     "trait_type": "background",
     "value": "green"
    },
    {
     "trait_type": "fur",
     "value": "purple"
    },
    {
     "trait_type": "Mouth",
     "value": "other smile"
  
   ]
  },
  "2": {
   "name": "digi#2",
   "description": "DIGI",
   "image": "//asset/2",
   "attributes": [
    {
     "trait_type": "background",
     "value": "purple"
    },
    {
     "trait_type": "fur",
     "value": "red"
    },
   ]
  },

Using Python I wish to read from the JSON file I have and from each object create a new JSON file with the object name containing only that object data. I don't have JQ and don't know how to use that so need to be able to write it in Python.

I know somehow I'm to then use dump() to read each object and output each object to a new file but i can't figure it out

import json

with open('metadata1.json') as json_file:
    data = json.load(json_file)
    data = json.dumps(data, indent=4)

CodePudding user response:

You need to iterate over keys and values and dump value in separate file, named as key.

import json

with open('metadata1.json') as json_file:
    data = json.load(json_file)
    for key, value in data.items():
        with open(f'{key}.json', 'w') as f:
            json.dump(value, f, indent=4)

CodePudding user response:

Your json file content you provided is not valid json. You are missing a few closing brackets }, also you need to remove the final , at the end of lists.

Should read as follows

{
  "0": {
    "name": "DIGI#0",
    "description": "DIGI",
    "image": "//asset/0",
    "attributes": [
      {
        "trait_type": "background",
        "value": "pink"
      },
      {
        "trait_type": "fur",
        "value": "light grey"
      },
      {
        "trait_type": "Mouth",
        "value": "yawn"
      },
      {
        "trait_type": "clothes",
        "value": "suit"
      },
      {
        "trait_type": "accessories",
        "value": "hat"
      }
    ]
  },
  "1": {
    "name": "DIGI#1",
    "description": "DIGI",
    "image": "//asset/1",
    "attributes": [
      {
        "trait_type": "background",
        "value": "green"
      },
      {
        "trait_type": "fur",
        "value": "purple"
      },
      {
        "trait_type": "Mouth",
        "value": "other smile"
      }
    ]
  },
  "2": {
    "name": "digi#2",
    "description": "DIGI",
    "image": "//asset/2",
    "attributes": [
      {
        "trait_type": "background",
        "value": "purple"
      },
      {
        "trait_type": "fur",
        "value": "red"
      }
    ]
  }
}

Your file handling looks to be correct, you can access the json file like this:

import json

with open('metadata1.json') as json_file:
    data = json.load(json_file)  # will convert JSON file to python dictionary

# Access data like normal dictionary
name = data["0"]["name"]

# convert data back to json object and format it
print(json.dumps(data, indent=4))

CodePudding user response:

After loading, just itterate over the dictionary and save every object as a new file using json.dump

import json
with open("metadata1.json") as f:
    data = json.load(f)

for item in data.items():
    with open(f"object-{item[0]}.json", "w") as f:
        json.dump(item[1], f, indent=4)
  • Related