Uncategorized

Simplify JSON to help CSV conversion, python


I have a JSON file that I want to convert to CSV.
I’m doing it with:

import pandas as pd
with open('test.json', encoding='utf-8') as inputfile:
    df = pd.read_json(inputfile)
df.to_csv('test.csv', encoding='utf-8', index=False)

Everything works but my JSON has structure that I want to “clean” to make conversion nicer.
My structure is:

"field A": 117,
"field B": 143,
"field C": 27,
"field D": [
    {
    "id": 782,
    "name": "Some test A",
    "type": "Group"
    }
],
"field E": null,
"field F": "contact",
"field G": [
    {
    "id": 32358,
    "name": "Some test B",
    "type": "Note"
    },
    {
    "id": 37557,
    "name": "Some test C",
    "type": "Note"
    },
    {
    "id": 38416,
    "name": "Some test D",
    "type": "Note"
    }
],
"field H": null,

I would like to remove all “id” and “type” leaving only “name” and if multiple names exsists have them each on new line.

"field A": 117,
"field B": 143,
"field C": 27,
"field D": "Some test A",
"field E": null,
"field F": "contact",
"field G": "Some test B \n Some test C \n Some test D",
"field H": null,

Any suggestions how to do it?
Thank you!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *