0

I have this json response, consisting of unicode dictionary and having trouble to convert it as a python dictionary:

u'{
    "event": {
        "event_type": "signature_request_sent",
        "event_time": "1407227830",
        "event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337",
        "event_metadata": {
            "related_signature_id": null,
            "reported_for_account_id": null,
            "reported_for_app_id": "290e6bb260fed5613b68066084fc77ff",
            "event_message": null
        }
    },
    "account_guid": null,
    "client_id": "290e6bb260fed5613b68066084fc77ff",
    "signature_request": {

        "title": "My First embedded signature request with a template",
        "original_title": "My First embedded signature request with a template",
        "subject": "My First embedded signature request with a template",
        "message": "Awesome, right?",
        "test_mode": true,
        "is_complete": false,
        "has_error": false,
        "custom_fields": [

        ],
        "response_data": [

        ],
        "signing_url": null,
        "signing_redirect_url": null,
        "final_copy_uri": "",
        "signatures": [
            {
                "signature_id": "9c5d829af9e0566134dd28bd064a2fe8",
                "has_pin": false,
                "signer_email_address": "napopasas@gmail.com",
                "order": null,
                "status_code": "awaiting_signature",
                "signed_at": null,
                "last_viewed_at": null,
                "last_reminded_at": null
            }
        ],
        "cc_email_addresses": [

        ]
    }
}'

tried following solution without success (ValueError for string)

Convert unicode string dictionary into dictionary in python

Cœur
  • 37,241
  • 25
  • 195
  • 267
navyad
  • 3,752
  • 7
  • 47
  • 88

2 Answers2

0

Use json.loads()

>>> a = """{
...     "event": {
...         "event_type": "signature_request_sent",
...         "event_time": "1407227830",
...         "event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337",
...         "event_metadata": {
...             "related_signature_id": null,
...             "reported_for_account_id": null,
...             "reported_for_app_id": "290e6bb260fed5613b68066084fc77ff",
...             "event_message": null
...         }
        "original_title": "My First embedded signature request with a template",
...     },
...     "account_guid": null,
...     "client_id": "290e6bb260fed5613b68066084fc77ff",
...     "signature_request": {
...
...         "title": "My First embedded signature request with a template",
...         "original_title": "My First embedded signature request with a template",
...         "subject": "My First embedded signature request with a template",
...         "message": "Awesome, right?",
...         "test_mode": true,
...         "is_complete": false,
...         "has_error": false,
...         "custom_fields": [
...
...         ],
...         "response_data": [
...
...         ],
...         "signing_url": null,
...         "signing_redirect_url": null,
...         "final_copy_uri": "",
                "signature_id": "9c5d829af9e0566134dd28bd064a2fe8",
...         "signatures": [
...             {
...                 "signature_id": "9c5d829af9e0566134dd28bd064a2fe8",
...                 "has_pin": false,
...                 "signer_email_address": "napopasas@gmail.com",
...                 "order": null,
...                 "status_code": "awaiting_signature",
...                 "signed_at": null,
...                 "last_viewed_at": null,
...                 "last_reminded_at": null
...             }
...         ],
...         "cc_email_addresses": [
...
...         ]
...     }
... }"""
>>> import json
>>> import pprint
>>> pprint.pprint(json.loads(a))
{u'account_guid': None,
 u'client_id': u'290e6bb260fed5613b68066084fc77ff',
 u'event': {u'event_hash': u'567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337',
            u'event_metadata': {u'event_message': None,
                                u'related_signature_id': None,
                                u'reported_for_account_id': None,
                                u'reported_for_app_id': u'290e6bb260fed5613b68066084fc77ff'},
            u'event_time': u'1407227830',
            u'event_type': u'signature_request_sent'},
 u'signature_request': {u'cc_email_addresses': [],
                        u'custom_fields': [],
                        u'final_copy_uri': u'',
                        u'has_error': False,
                        u'is_complete': False,
                        u'message': u'Awesome, right?',
                        u'original_title': u'My First embedded signature request with a template',
                        u'response_data': [],
                        u'signatures': [{u'has_pin': False,
                                         u'last_reminded_at': None,
                                         u'last_viewed_at': None,
                                         u'order': None,
                                         u'signature_id': u'9c5d829af9e0566134dd28bd064a2fe8',
                                         u'signed_at': None,
                                         u'signer_email_address': u'napopasas@gmail.com',
                                         u'status_code': u'awaiting_signature'}],
                        u'signing_redirect_url': None,
                        u'signing_url': None,
                        u'subject': u'My First embedded signature request with a template',
                        u'test_mode': True,
                        u'title': u'My First embedded signature request with a template'}}
>>>
Nilesh
  • 20,521
  • 16
  • 92
  • 148
0
import json
str1 = '{"event": {"event_type": "signature_request_sent","event_time": "1407227830","event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337","event_metadata": {"related_signature_id": null,"reported_for_account_id": null,"reported_for_app_id": "290e6bb260fed5613b68066084fc77ff","event_message": null}},"account_guid": null,"client_id": "290e6bb260fed5613b68066084fc77ff","signature_request": {"title": "My First embedded signature request with a template","original_title": "My First embedded signature request with a template","subject": "My First embedded signature request with a template","message": "Awesome, right?","test_mode": true,"is_complete": false,"has_error": false,"custom_fields": [],"response_data": [],"signing_url": null,"signing_redirect_url": null,"final_copy_uri": "","signatures": [{"signature_id": "9c5d829af9e0566134dd28bd064a2fe8","has_pin": false,"signer_email_address": "napopasas@gmail.com","order": null,"status_code": "awaiting_signature","signed_at": null,"last_viewed_at": null,"last_reminded_at": null}],"cc_email_addresses": []}}'
js1 = json.loads(str1)
print type(js1)

This will give you a python dict. use json.loads() to convert your json to python dict. Hope this is helpful to you

Sesha
  • 202
  • 1
  • 5