-2

I have the following JSON:

"item": [
            {
                "id": "3",
                "num": 0,
                "name": "de",
                "leve": {
                    "label": [
                        "Ini",
                        "Sec",
                        "Coo"
                    ],
                    "effect": [
                        "{{ e5 }} -> {{ e5NL }}",
                        "{{ e1 }} -> {{ e1NL }}",
                        "{{ cooldown }} -> {{ cooldownnNL }}"
                    ]
                },
            },
            {
                "id": "4",
                "num": 0,
                "name": "de",
                "leveltip": {
                    "label": [
                        "Initial Damage",
                        "Secondary Damage",
                        "Cooldown"
                    ],
                    "effect": [
                        "{{ e5 }} -> {{ e5NL }}",
                        "{{ e1 }} -> {{ e1NL }}",
                        "{{ cooldown }} -> {{ cooldownnNL }}"
                    ]
                },
            },
            {
                "id": "5",
                "num": 0,
                "name": "de",
                "leveltip": {
                    "label": [
                        "Initial Damage",
                        "Secondary Damage",
                        "Cooldown"
                    ],
                    "effect": [
                        "{{ e5 }} -> {{ e5NL }}",
                        "{{ e1 }} -> {{ e1NL }}",
                        "{{ coo}} -> {{ cooNL }}"
                    ]
                },
            },
        ],

I want to save this into csv file which contains all data into a seperate table like below :

item_id item_name item_num item_level_label_inti item_level_effect_inti

 3        de        0       ini                   {{ e5 }} -> {{ e5NL }}
 4        de        0       ini                   {{ e5 }} -> {{ e5NL }}

like above column its contains all the data into a column

Sachin Jaiswal
  • 143
  • 1
  • 9

2 Answers2

0

Please see this post on the same problem

You are going to have a problem with converting because of nested json objects, so you will need to flatten those first and then convert it to CSV

Community
  • 1
  • 1
Wamadahama
  • 1,489
  • 13
  • 19
0

Here is the implementation but in your input id 3 has a diffrent key leve instaed of leveltip. So i consider that as a wrong entry.

import csv
file_name = 'out.csv'
data = [(item['id'],item['name'],item['num'],item['leveltip']['label'][0],item['leveltip']['effect'][0]) for item in maindict['item']]
with open(file_name, "wb") as csv_file:
    writer = csv.writer(csv_file, delimiter=',')
    writer.writerow(["item_id","item_name","item_num","item_level_label_inti","item_level_effect_inti"])
        for line in data:
            writer.writerow(line)

Working

Create a list of tuples which collect all the data in required format then write on the csv file using csv package.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52