Skip to content

Instantly share code, notes, and snippets.

@TheDanishDynamo
Created January 19, 2019 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheDanishDynamo/454a17c8c83ed30d9aec1de8b8861741 to your computer and use it in GitHub Desktop.
Save TheDanishDynamo/454a17c8c83ed30d9aec1de8b8861741 to your computer and use it in GitHub Desktop.
Sample json
{
"totalSize": 2,
"done": true,
"records": [
{
"attributes": {
"type": "Account",
"url": "/services/data/v44.0/sobjects/Account/1"
},
"Name": "strata"
},
{
"attributes": {
"type": "Account",
"url": "/services/data/v44.0/sobjects/Account/2"
},
"Name": "dup1"
}
]
}
@mvanorder
Copy link

mvanorder commented Jan 19, 2019

mvanorder@mvanorde-c4g39:~$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
json_string = """{
...     "totalSize": 2,
...     "done": true,
...     "records": [
...         {
...             "attributes": {
...                 "type": "Account",
...                 "url": "/services/data/v44.0/sobjects/Account/1"
...             },
...             "Name": "strata"
...         },
...         {
...             "attributes": {
...                 "type": "Account",
...                 "url": "/services/data/v44.0/sobjects/Account/2"
...             },
...             "Name": "dup1"
...         }
...     ]
... }"""
json_string
'{\n    "totalSize": 2,\n    "done": true,\n    "records": [\n        {\n            "attributes": {\n                "type": "Account",\n                "url": "/services/data/v44.0/sobjects/Account/1"\n            },\n            "Name": "strata"\n        },\n        {\n            "attributes": {\n                "type": "Account",\n                "url": "/services/data/v44.0/sobjects/Account/2"\n            },\n            "Name": "dup1"\n        }\n    ]\n}'
import json
json.loads(json_string)
{'done': True, 'records': [{'attributes': {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/1'}, 'Name': 'strata'}, {'attributes': {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/2'}, 'Name': 'dup1'}], 'totalSize': 2}
json_data = json.loads(json_string)
json_data['records']
[{'attributes': {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/1'}, 'Name': 'strata'}, {'attributes': {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/2'}, 'Name': 'dup1'}]
list(item for item in json_data['records'])
[{'attributes': {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/1'}, 'Name': 'strata'}, {'attributes': {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/2'}, 'Name': 'dup1'}]
list(item['attributes'] for item in json_data['records'])
[{'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/1'}, {'type': 'Account', 'url': '/services/data/v44.0/sobjects/Account/2'}]
list(item['attributes']['url'] for item in json_data['records'])
['/services/data/v44.0/sobjects/Account/1', '/services/data/v44.0/sobjects/Account/2']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment