0

I have a JSON dict in Python which I would like to parse into a CSV, my data and code looks like this:

import csv
import json

x = {
"success": 1,
"return": {
    "variable_id": {
        "var1": "val1",
        "var2": "val2"
    }...

f = csv.writer(open("foo.csv", "w", newline=''))
for x in x:
    f.writerow([x["success"],
                '--variable value--',
                x["return"]["variable_id"]["var1"],
                x["return"]["variable_id"]["var2"])

However, since variable_id's value is going to change I don't know how to refer to in the code. Apologies if this is trivial but I guess I lack the terminology to find the solution.

1
  • 1
    x is not a JSON object, it's just a dict Commented Aug 10, 2017 at 13:05

2 Answers 2

3

You can use the * (unpack) operator to do this, assuming only the values in your variable_id matter :

f.writerow([x["success"],
           '--variable value--',
           *[val for variable_id in x['return'].values() for val in variable_id.values()])

The unpack operator essentially takes everything in x["return"]["variable_id"].values() and appends it in the list you're creating as input for writerow.

EDIT this should now work if you don't know how to referencevariable_id. This will work best if you have several variable_ids in x['return'].

If you only have one variable_id, then you can also try this :

f.writerow([x["success"],
           '--variable value--',
           *list(x['return'].values())[0].values()])

Or

f.writerow([x["success"],
           '--variable value--',
           *next(iter(x['return'].values())).values()])
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, perhaps I was unclear but the issue is that I cannot use "variable_id", as it is not a constant, it will change for each x["return"], hence I don't know how to reference it. Perhaps there is a kind of placeholder/variable syntax I'm unaware of?
I've updated my answer so it would work if the key(s) in x['return'] is(are) not constant(s). Something better may exist, but it should work. Of course, you can (should) use a generator instead of a list comprehension.
Thank you, this now works, except for it writes everything to the same row, any idea how to make it write each variable_id on separate ones?
You could probably do this in a single line : f.writerows([[x['success'], '--variable value--', *vals] for vals in x['return'].values()]) or something like that.
1

You can get variable_id's value using x['success']['return'].keys[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.