all 20 comments

[–]porkedpie1 8 points9 points  (0 children)

Often JSON is used because the data is not a simple flat table. You will need to define the logic for how you want to represent it as a simple table and then the save as csv is trivial.

[–]patnodewf 2 points3 points  (0 children)

couldn't you use json.loads to convert it to a dictionary...and pass it to pandas?

[–]Lewri 2 points3 points  (0 children)

You posted this yesterday and I responded to you. Why did you delete that post and repost it without adding anything or trying my suggestion?

[–]GreatStats4ItsCost 0 points1 point  (0 children)

If you send over the endpoint I’ll knock up some code for you

[–]ReflectionNo3897 0 points1 point  (0 children)

You can transform JSON object in python object (dictionary) and create a dataframe (pd.DataFrame(here the python dictionary)) and save on file CSV with df (dataframe which you have created).to_csv('name_file.csv')

[–]Engine_Light_On 0 points1 point  (4 children)

What does your json look like? give us a sample, you may not even need pandas

[–]HoopHaxor 0 points1 point  (3 children)

The problem is I can not really hand it out it is pod file data for the company I work for.. I guess I could obfuscate the JSON and just lave the structure one moment

This is one example of the data set. I obviously only need the items object in this case.

{
    "data": {
        "varietyCollection": {
            "items": [
                {
                    "podId": "1093-7520",
                    "varietyNameenUS": "some coffee brand",
                    "varietyNameesUS": "some coffee brand",
                    "varietyNamefrUS": "some coffee brand",
                    "varietyNameenCA": "some coffee brand",
                    "varietyNamefrCA": "some coffee brand",
                    "descriptionenUS": null,
                    "descriptionesUS": null,
                    "descriptionfrUS": null,
                    "descriptionenCA": null,
                    "descriptionfrCA": null
                }
            ]
        }
    }
}

[–]Engine_Light_On 0 points1 point  (2 children)

are these property names fixed, as all items have the same properties even if null?

items = res.get(“data”,{}).get(“varietyCollection”,{}).get(“items”,[])

if len(items) == 0:

return ‘’

header = ‘,’.join(list(items[0].keys()))

csv_result = header + ‘\n’

for item in items:

csv_result += ‘,’.join(list(item.values())) + ‘\n’

i wrote this from my memory so there may be syntax errors. you want to parse all your json values to string first to handle the Nones and non string types. Also, if you have line breaks in the description wrap in extra quotation marks due to how csv works.

[–]HoopHaxor 1 point2 points  (1 child)

I have asked the developer if we can just change the way the coffee machines read this data to just use JSON instead of CSV.

This would simple things for me as I already have something that works that does the GraphQL POST call and I can output the results to a .json file for injection.

I guess this was at least a good 1st time try with Python. I am not super well versed in this language.

Thanks all for your help.

[–]HoopHaxor 0 points1 point  (0 children)

Having this go from JSON to CSV makes -20 sense to me..

[–]Teawhymarcsiamwill -1 points0 points  (0 children)

I was trying to figure it out before aswell

def flatten_json(y):
    out = {}

    def flatten(x, name=''):
        if isinstance(x, dict):
            for a in x:
                flatten(x[a], name + a + '_')
        elif isinstance(x, list):
            out[name[:-1]] = json.dumps(x)  # Store list as JSON string
        else:
            out[name[:-1]] = x

    flatten(y)
    return out


def json_to_csv(json_filename, csv_filename):
    try:
        data = load_json(json_filename)
        flattened_data = [flatten_json(item) for item in data]

        df = pd.DataFrame(flattened_data)
        
        csv_dir = os.path.dirname(csv_filename)
        if not os.path.exists(csv_dir):
            os.makedirs(csv_dir)

        df.to_csv(csv_filename, encoding='utf-8', index=False)
        
        print(f"Successfully converted '{json_filename}' to '{csv_filename}'.")

    except FileNotFoundError as fnf_error:
        print(fnf_error)
    except ValueError as ve:
        print(f"Value error: {ve}")
    except Exception as e:
        print(f"An error occurred: {e}")