3

I am trying to follow the answer(s) in the question shown here, but I'm still having some trouble combining my two json objects.

I have two JSON objects which are returned from a web call and I'm storing them in variables called: likes_data and comments_data. Each of these are currently empty JSON objects (in my test cases only) and when printed to the screen show: {"data":[]}.

Now I would like to combine these two (sometimes empty) JSON objects into a single object and print it to the screen, but I'm having some trouble because the final combined object always has escaped quotes in it.

The code:

data = { 'likes' : likes_data, 'comments' : comments_data }
self.response.out.write(json.dumps(data))

results in:

{
  "likes": "{\"data\":[]}", 
  "comments": "{\"data\":[]}"
}

which is obviously an incorrectly formatted JSON response because of the escaped quotations.

Is there a proper way to combine two JSON objects in Python? for the simple case, I can manually unescape these, but I'd like to be able to manage "data" objects which are more complex.

Does anyone have any advice?

Cheers, Brett

2
  • why not parse likes_data and comments_data and then put them into data as objects? Commented Aug 24, 2012 at 14:26
  • JSON is javascript object notation allowing data to be transmitted over a network or two different systems, it is transmitted as text and it is text to all other languages. You should convert json data to a python object and do your things, then encode it into json before you send; fee Fabian's example. Commented Aug 24, 2012 at 14:30

1 Answer 1

12
likes = json.loads(likes_data)
comments = json.loads(comments_data)
data = {'likes': likes['data'], 'comments': comments['data']}
self.response.out.write(json.dumps(data))

Like this?

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

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.