1

I am trying to make a list of JSON objects in python I have an list of keys, and a list of items, so keys is something like this, by the way I am getting this list from the web so it is always a single quote

keys = ['one', 'two', 'three']

and then items something like this

rows = [[foo, fuu, fhh], [bar, bat, bak]]

And what I want is

[{"one":"foo", "two":"fuu", "three":"fhb"},
{"one":"bar", "two":"bat", "three":"bak"}]

And here is what I am trying but I end up with this

['{"one":"foo", "two":"fuu", "three":"fhb"}',
'{"one":"bar", "two":"bat", "three":"bak"}']

Which makes it no longer valid JSON:

results = []
info = {}
for row in items:
    i = 0
    for item in row:
        info[keys[i]] = item
        data = json.dumps(info)
        i += 1
    results.append(data)

So how can I get rid of those single quotes and just have double quotes and valid JSON?

Thanks

1
  • You're making a list of strings. Make a list, then dumps it. Commented Aug 5, 2015 at 19:23

2 Answers 2

1

Move the json.dumps at the end, not at each iteration:

results = []
for row in items:
    i = 0
    info = {}
    for item in row:
        info[keys[i]] = item
        i += 1
    results.append(info)

return json.dumps(results)

It will first construct your list of strings and then serialize the Python object as a JSON one.

Sign up to request clarification or add additional context in comments.

3 Comments

okay now that I have after the json.dumps I get this final_result =[{"one":"foo", "two":"fuu", "three":"fhb"}, {"one":"bar", "two":"bat", "three":"bak"}] how could I just get the first JSON because final_result[0] doesn't work?
I don't understand what you want then. Why do you want a JSON object if it is for accessing items after via Python syntax? results is a Python object: you can do results[0]['one'] (which returns foo). With json.dump(results) you get a JSON representation of the list of dict. You need to do json.loads before to be able to use it as a Python object again.
@spenf10 Agree with Maxime. If you're going to try to use the data when you're done, you don't want JSON. You just want the list of dicts without converting to JSON. JSON is strictly text data, usually used for exchanging data between two different systems (like between a web application and a browser).
1

When you use json.dumps you create a json string that can be written to a file that represents your data. You should create the list and then, at the end, use json.dumps to create your json.

You could also use zip to make your code more readable:

>>> results = [dict(zip(keys, row)) for row in rows]
>>> print results

[{'one': 'foo', 'three': 'fhh', 'two': 'fuu'},
 {'one': 'bar', 'three': 'bak', 'two': 'bat'}]

>>> json.dumps(results)

'[{"three": "fhh", "two": "fuu", "one": "foo"}, {"three": "bak", "two": "bat", "one": "bar"}]'

1 Comment

Much more Pythonic than the other answer.

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.