0

I use the json module and the dumps method to obtain a string which represents a list of json objects :

import json
jsonstring = json.dumps(data)

I would like to iterate over this string to obtain each JSON object as a string.

Any suggestions?

Thanks in advance.

P.S. I have tried the following:

for jsonobject in jsonstring:
    print jsonobject

But what happens is that each single letter is printed separately rather than the jsonobject as a whole.

1
  • what are you trying to accomplish? json.dumps() will return a single string representation of 'data' Commented Oct 27, 2010 at 14:59

2 Answers 2

3
for jsonobject in json.loads(jasonstring):
    print jsonobject
Sign up to request clarification or add additional context in comments.

1 Comment

thanks john. this does it... and jsonobject is a unicoded string.
1

You should iterate over your data before you turn it into a string, then turn each element of the data into JSON:

for d in data:
    jsonstring = json.dumps(d)

1 Comment

thanks ned! that does exactly what i need and didn't realize it. sorry about the noobiness...

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.