# REST API Call ---Extract Value from Specific Key from Response

**URL:** <https://discuss.python.org/t/rest-api-call-extract-value-from-specific-key-from-response/43861>\
**Category:** Python Help\
**Tags:** help\
**Created:** [January 22, 2024, 2:11pm UTC](https://discuss.python.org/t/rest-api-call-extract-value-from-specific-key-from-response/43861 "2024-01-22T14:11:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericdavishop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@ericdavishop](https://discuss.python.org/u/ericdavishop)\
**Post date:** [January 22, 2024, 2:11pm UTC](https://discuss.python.org/t/rest-api-call-extract-value-from-specific-key-from-response/43861/1 "2024-01-22T14:11:05Z")

</div>

```auto
response = requests.request("GET", url, headers=headers, data=payload, verify=False)

```

My response variable contains a response from an HTTP request and I need to extract the ID value under the Items array.

```auto
{'links': {'self': 'https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords?offset=0&limit=25&filter=name:Test-FTD&expanded=true'}, **'items': [{'deploymentStatus': 'DEPLOYMENT_PENDING', 'id': 'adab761e-9668-11ee-abae-e49d35beb127'**, 'type': 'Device', 'links': {'self': 'https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11eember': '78', 'modelType': 'Sensor', 'healthStatus': 'red', 'healthMessage': 'Cisco Cloud Configuration - SSE enrollment failure. ', 'sw_version': 
'7.2.5', 'healthPolicy': {'id': 'dbe98880-32e2-11ed-917a-e6821804acf4', 'type': 'HealthPolicy', 'name': 'Initial_Health_Policy 2022-09-12 21:35:45'}, 'accessPolicy': {'name': 'Test-Policy', 'id': '0050568a-7152-0ed3-0000-012884920695', 'type': 'AccessPolicy'}, 'advanced': {'enableOGS': False}, 'hostName': '10.160.5.6', 'license_caps': ['BASE', 'THREAT'], 'keepLocalEvents': False, 'prohibitPacketTransfer': True, 'isConnected': True, 'ftdMode': 'ROUTED', 'analyticsOnly': False, 'deviceGroup': {'id': '95c4f0e4-a1b7-11ed-b5ed-82df665b6681', 'type': 'DeviceGroup', 'name': 'US'}, 'metadata': {'readOnly': {'state': False}, 'inventoryData': {'cpuCores': '1 CPU (4 cores)', 'cpuType': 'CPU Atom C3000 series 2200 MHz', 'memoryInMB': '2824 MB RAM'}, 'deviceSerialNumber': 'JAD2749031Y', 'domain': {'name': 'Global', 'id': 'e276abec-e0f2-11e3-8169-6d9ed49b625f', 'type': 'Domain'}, 'isMultiInstance': False, 'snortVersion': '3.1.21.500-21', 'vdbVersion': 'Build 353 - 2022-03-07 22:13:19.0', 'lspVersion': 'lsp-rel-20240117-1601', 'clusterBootstrapSupported': False}, 'snortEngine': 'SNORT3'}], 'paging': {'offset': 0, 'limit': 25, 'count': 1, 'pages': 1}}

```

I am sorta new to coding so any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [January 23, 2024, 1:23am UTC](https://discuss.python.org/t/rest-api-call-extract-value-from-specific-key-from-response/43861/2 "2024-01-23T01:23:44Z")

</div>

The response you are seeing is JSON-formatted data. Use the `.json` method of the response to parse it; the [documentation shows an example right at the top](https://docs.python-requests.org/en/latest/index.html).

Once you have parsed JSON data, you have a **perfectly ordinary** nested structure of **perfectly ordinary** Python `dict`s and `list`s (we don’t call them “arrays” in Python, although the JSON format description does), which you work with **exactly the same way** as if you had gotten that data by any other means.

So, suppose we have done something like:

```auto
data = requests.get(url, headers=headers, data=payload, verify=False).json()

```

Now `data` is a `dict` that has a `'links'` key, where the value is another dict that has the `'items'` key that you’re interested in; then the value there is a list, and then each element (although _in this case_ there happens to be exactly one element) is a dict with an `'id'` key.) To access that, we simply step through those keys and elements, one at a time: `data['links']['items'][0]['id']`.

That is, assuming I read it correctly 😉 (As a side note: please don’t try to add bold highlighting inside a code block on the forum - it _doesn’t work_, and just leaves extra asterisks in place that can cause confusion.)

But before we can write the code, **we must carefully consider what the code should do if** the list _doesn’t_ contain exactly one element. The API designer made the JSON data use an array here (which turns into a list in the Python result) for a reason. Presumably, there could be more than one item, or no items at all (which is presumably also why the key is named `'items'`, and not `'item'`). So - if you want _all_ the "ID"s from the items, you could use a list comprehension, or write a loop, to process the list of items. If you want a specific one, you could index in normally. If you want to make sure there’s exactly one element first (and maybe raise an exception otherwise), then do that. Etc.

---

<div class="post-metadata">

**Author:** ![ericdavishop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@ericdavishop](https://discuss.python.org/u/ericdavishop)\
**Post date:** [January 23, 2024, 11:32am UTC](https://discuss.python.org/t/rest-api-call-extract-value-from-specific-key-from-response/43861/3 "2024-01-23T11:32:36Z")

</div>

Karl,

Thank you so much for taking the time walk me through that. I appreciate the explanation of the distinction between JSON data types and Python data types. And yes, I did notice the “Bold” feature did not work as intended. You probably know my intention was to highlight the elements I was interesting in obtaining from the result. Next time I will just leave it as a simple code block. I will take what you explained and work on this today and reply back with my results…successful results!!..lol
