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.

Community
  • 1
  • 1
Shashank
  • 452
  • 1
  • 8
  • 26
  • 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). – Hyperboreus Oct 04 '13 at 19:12
  • I try to use json.loads...like store response in variable and iterate in loop: `code`result = [ json.loads(resp) for resp in data ]`code` and i get an error: `code`ValueError: Unterminated string starting at: line 1 column 104 (char 104)`code` – Shashank Oct 05 '13 at 04: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. – Hyperboreus Oct 05 '13 at 05: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 – Shashank Oct 05 '13 at 05:34

2 Answers2

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)
Ludwik Trammer
  • 23,375
  • 6
  • 65
  • 90
Hyperboreus
  • 31,109
  • 9
  • 45
  • 84
  • 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. – Shashank Oct 05 '13 at 04:32
  • 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 – Shashank Oct 05 '13 at 04:48
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']
Abhijit
  • 59,056
  • 16
  • 119
  • 195
  • 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.. – Shashank Oct 05 '13 at 04:45
  • 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... – Shashank Oct 05 '13 at 10:25