Json to CSV Conversion
<p>I have a very large JSON file with multiple individual JSON objects in the format shown below. I am trying to convert it to a CSV so that each row is a combination of the outer id/name/alphabet in a JSON object and 1 set of conversion: id/name/alphabet. This is repeated for all the sets of id/name/alphabet within an individual JSON object. So from the object below, 2 rows should be created where the first row is (outer) id/name/alphabet and 1st id/name/alphabet of conversion. The second row is again (outer) id/name/alphabet and now the 2nd id/name/alphabet of conversion.</p>

<p>Important note is that certain Objects in the file can have upwards of 50/60 conversion id/name/alphabet pairs.</p>

<p>What I tried so far was to flatten the JSON objects first which resulted in keys like conversion_id_0 and conversion_id_1 etc... so I can map the outer as its always constant but I am unsure how to map each corresponding number set to be a seperate row.</p>

<p>Any help or insight would be greatly appreciated!</p>

<pre><code>[
    {
        "alphabet": "ABCDEFGHIJKL",
        "conversion": [
            {
                "alphabet": "BCDEFGHIJKL",
                "id": 18589260,
                "name": [
                    "yy"
                ]
            },
            {
                "alphabet": "EFGHIJEFGHIJ",
                "id": 18056632,
                "name": [
                    "zx",
                    "cd"
                ]
            }
        ],
        "id": 23929934,
        "name": [
            "x",
            "y"
        ]
    }
]
</code></pre>

<p>Your question is unclear about exactly the mapping from input JSON data to rows of the CSV file, so I had to guess on what should happen when there's more than one "name" associated with an inner or outer object.</p>

<p>Regardless, hopefully the following will give you a general idea of how to solve such problems.</p>

<pre><code>import csv

objects = [
    {
        "alphabet": "ABCDEFGHIJKL",
        "id": 23929934,
        "name": [
            "x",
            "y"
        ],
        "conversion": [
            {
                "alphabet": "BCDEFGHIJKL",
                "id": 18589260,
                "name": [
                    "yy"
                ]
            },
            {
                "alphabet": "EFGHIJEFGHIJ",
                "id": 18056632,
                "name": [
                    "zx",
                    "cd"
                ]
            }
        ],
    }
]

with open('converted_json.csv', 'wb') as outfile:
    def group(item):
        return [item["id"], item["alphabet"], ' '.join(item["name"])]

    writer = csv.writer(outfile, quoting=csv.QUOTE_NONNUMERIC)
    for obj in objects:
        outer = group(obj)
        for conversion in obj["conversion"]:
            inner = group(conversion)
            writer.writerow(outer + inner)
</code></pre>

<p>Contents of the CSV file generated:</p>

<pre class="lang-none prettyprint-override"><code>23929934,"ABCDEFGHIJKL","x y",18589260,"BCDEFGHIJKL","yy"
23929934,"ABCDEFGHIJKL","x y",18056632,"EFGHIJEFGHIJ","zx cd"
</code></pre>

python json csv