2

This is my response code from GCM-python,

{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b
9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"erro
r":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"er
ror":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865
596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}

When i get this response, i want to collect all error keys from dictionary...but it seems like it is a string and i'll try dump in json using json.dumps() and then remove slashes but not working for me, not even ast working. I try this one python json dumps. What am i missing there? please help me in this.

4
  • 5
    If it is indeed a string, use json.loads not json.dumps. Dump = From data object to string (serialization); load = from string to data object (deserialization). Commented Oct 4, 2013 at 19:12
  • I try to use json.loads...like store response in variable and iterate in loop: coderesult = [ json.loads(resp) for resp in data ]code and i get an error: codeValueError: Unterminated string starting at: line 1 column 104 (char 104)code Commented Oct 5, 2013 at 4:29
  • Reading your question and especially reading your comments to the answers given, if you don't even know whether your input is a string or something else, really limits anybody's ability to help you. Commented Oct 5, 2013 at 5:10
  • I know what i am asking, now i explain my problem to the point where i am stuck....u can also show in code, the output is string and not able to convert in list, pardon me please if i move in wrong way Commented Oct 5, 2013 at 5:34

2 Answers 2

4

If it is a string, load it, don't dump it:

#! /usr/bin/python3

import json

a = '''{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}'''

j = json.loads(a)
errors = [d for d in j ['results'] if 'error' in d]
print(errors)
Sign up to request clarification or add additional context in comments.

2 Comments

Initially I try to load it but it shows an error: ValueError: Unterminated string starting at: line 1 column 104 (char 104), so first i dump it and then load it because json gives me an error.
I got my response when python GCM returns a response object, and then i iterate and store data in a list or try to make a list from json.loads in loop but no work
2

As the data you receive is a valid Python data, you can simply use [ast.literal_eval][1]

Demo

import ast
data = '''{"multicast_id":6343554431392278573,"success":5,"failure":15,"canonical_ids":0,"results":[{"message_id":"0:1380910865603840%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":
"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865592683%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1380910865600910%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865596592%356b9054f9fd7ecd"},{"error":"NotRegistered"},{"message_id":"0:1380910865595499%356b9054f9fd7ecd"}]}'''

>>> pp.pprint(ast.literal_eval(data))
{   'canonical_ids': 0,
    'failure': 15,
    'multicast_id': 6343554431392278573L,
    'results': [   {   'message_id': '0:1380910865603840%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865592683%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865600910%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865596592%356b9054f9fd7ecd'},
                   {   'error': 'NotRegistered'},
                   {   'message_id': '0:1380910865595499%356b9054f9fd7ecd'}],
    'success': 5}
>>> 

Followed by dumping the errors

>>> pp.pprint([elem['error'] for elem in ast.literal_eval(data)['results'] if 'error' in elem])
[   'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered',
    'NotRegistered']

2 Comments

It looks pretty awesome answer, but i got some problem,here what i do: GCM gives me response object which contain http status code, and when i iterate it like this: data = for resp in response print resp, then it shows me the dictionary which contain strings. Now i am try to apply ur solution but it gives me error "SyntaxError: EOL while scanning string literal", it cluttered my response in string chunks..
hey thanks for this solution, actually my data is not proper string...so first i make it string like d = (''.join('' + resp + '' for resp in g_response)) and then apply ast...

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.