解析 python 中的嵌套 json

Parsing a nested json in python

我正在尝试解析 json 的一小段摘录,以从 _source 中搜索和检索项目 securityId、enabled、productType、expiry 和 lastTradingDate。

{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 36450, "max_score": 5.2289367, "hits": [ { "_index": "securitylisting", "_type": "securitylistingtype", "_id": "1238346721692487680", "_score": 5.2289367, "_source": { "id": 1238346721692487700, "assetType": "FUTURE", "exchangeCode": "CME", "securityCode": "NG", "securityId": "NGG2", "enabled": true, "createdTimestampUtc": "2022-03-09T02:11:19.898Z", "updatedTimestampUtc": "2022-04-14T23:56:51.077Z", "productType": "OUTRIGHT", "currencyCode": "USD", "description": "Henry Hub Natural Gas Futures", "divisor": "3", "tickSize": "1", "tickValue": "10.0", "expiry": "2032-02-28", "lastTradingDate": "2032-01-28", "isFractionalContract": "false", "displayFactor": "0.001", "exchangeId": "2224171651002540032", "aliases": [ { "securityIdSource": "TT", "securityCode": "NG", "securityId": "NGG32" }, { "securityIdSource": "EXCHANGE", "securityCode": "NG", "securityId": "NGG2" } ], "protobuf": "CICggLqEud+XERABGgNDTUUiAk5HKgROR0cyShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDMyLTAyLTI4Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAzMi0wMS0yOEodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HRzMyUhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdHMg==\r\n" } }, { "_index": "securitylisting", "_type": "securitylistingtype", "_id": "1230533889064177664", "_score": 5.2289367, "_source": { "id": 1230533889064177700, "assetType": "FUTURE", "exchangeCode": "CME", "securityCode": "NG", "securityId": "NGN34", "enabled": true, "createdTimestampUtc": "2022-02-15T12:45:55.444Z", "updatedTimestampUtc": "2022-04-17T05:48:48.645Z", "productType": "OUTRIGHT", "currencyCode": "USD", "description": "Henry Hub Natural Gas Futures", "divisor": "3", "tickSize": "1", "tickValue": "10.0", "expiry": "2034-07-28", "lastTradingDate": "2034-06-28", "isFractionalContract": "false", "displayFactor": "0.001", "exchangeId": "2224171651002540032", "aliases": [ { "securityIdSource": "TT", "securityCode": "NG", "securityId": "NGN34" }, { "securityIdSource": "EXCHANGE", "securityCode": "NG", "securityId": "NGN34" } ], "protobuf": "CICggO7Wge+JERABGgNDTUUiAk5HKgVOR04zNEoXGgtwcm9kdWN0VHlwZSIIT1VUUklHSFRKExoM\r\nY3VycmVuY3lDb2RlIgNVU0RKLBoLZGVzY3JpcHRpb24iHUhlbnJ5IEh1YiBOYXR1cmFsIEdhcyBG\r\ndXR1cmVzSgwaB2Rpdmlzb3IiATNKDRoIdGlja1NpemUiATFKERoJdGlja1ZhbHVlIgQxMC4wShQa\r\nBmV4cGlyeSIKMjAzNC0wNy0yOEodGg9sYXN0VHJhZGluZ0RhdGUiCjIwMzQtMDYtMjhKHRoUaXNG\r\ncmFjdGlvbmFsQ29udHJhY3QiBWZhbHNlShYaDWRpc3BsYXlGYWN0b3IiBTAuMDAxSiEaCmV4Y2hh\r\nbmdlSWQiEzIyMjQxNzE2NTEwMDI1NDAwMzJSDyICVFQqAk5HMgVOR04zNFIVIghFWENIQU5HRSoC\r\nTkcyBU5HTjM0\r\n" } }, { "_index": "securitylisting", "_type": "securitylistingtype", "_id": "1185544030478405632", "_score": 5.2289367, "_source": { "id": 1185544030478405600, "assetType": "FUTURE", "exchangeCode": "CME", "securityCode": "NG", "securityId": "NGH6", "enabled": true, "createdTimestampUtc": "2021-10-14T09:12:17.286Z", "updatedTimestampUtc": "2022-04-17T05:48:49.130Z", "productType": "OUTRIGHT", "currencyCode": "USD", "description": "Henry Hub Natural Gas Futures", "divisor": "3", "tickSize": "1", "tickValue": "10.0", "expiry": "2026-03-25", "lastTradingDate": "2026-02-25", "isFractionalContract": "false", "displayFactor": "0.001", "exchangeId": "2224171651002540032", "aliases": [ { "securityIdSource": "TT", "securityCode": "NG", "securityId": "NGH26" }, { "securityIdSource": "EXCHANGE", "securityCode": "NG", "securityId": "NGH6" } ], "protobuf": "CICggJK7wPm5EBABGgNDTUUiAk5HKgROR0g2ShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDI2LTAzLTI1Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAyNi0wMi0yNUodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HSDI2UhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdINg==\r\n" } } ] } }

由于嵌套部分,我无法弄清楚如何正确解析它。任何指导表示赞赏。我检查了类似帖子的其他回复,但无法使用 json_data['output'][index']

解决
import json

product = '{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":36450,"max_score":5.2289367,"hits":[{"_index":"securitylisting","_type":"securitylistingtype","_id":"1238346721692487680","_score":5.2289367,"_source":{"id":1238346721692487680,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGG2","enabled":true,"createdTimestampUtc":"2022-03-09T02:11:19.898Z","updatedTimestampUtc":"2022-04-14T23:56:51.077Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2032-02-28","lastTradingDate":"2032-01-28","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGG32"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGG2"}],"protobuf":"CICggLqEud+XERABGgNDTUUiAk5HKgROR0cyShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDMyLTAyLTI4Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAzMi0wMS0yOEodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HRzMyUhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdHMg==\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1230533889064177664","_score":5.2289367,"_source":{"id":1230533889064177664,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGN34","enabled":true,"createdTimestampUtc":"2022-02-15T12:45:55.444Z","updatedTimestampUtc":"2022-04-17T05:48:48.645Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2034-07-28","lastTradingDate":"2034-06-28","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGN34"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGN34"}],"protobuf":"CICggO7Wge+JERABGgNDTUUiAk5HKgVOR04zNEoXGgtwcm9kdWN0VHlwZSIIT1VUUklHSFRKExoM\r\nY3VycmVuY3lDb2RlIgNVU0RKLBoLZGVzY3JpcHRpb24iHUhlbnJ5IEh1YiBOYXR1cmFsIEdhcyBG\r\ndXR1cmVzSgwaB2Rpdmlzb3IiATNKDRoIdGlja1NpemUiATFKERoJdGlja1ZhbHVlIgQxMC4wShQa\r\nBmV4cGlyeSIKMjAzNC0wNy0yOEodGg9sYXN0VHJhZGluZ0RhdGUiCjIwMzQtMDYtMjhKHRoUaXNG\r\ncmFjdGlvbmFsQ29udHJhY3QiBWZhbHNlShYaDWRpc3BsYXlGYWN0b3IiBTAuMDAxSiEaCmV4Y2hh\r\nbmdlSWQiEzIyMjQxNzE2NTEwMDI1NDAwMzJSDyICVFQqAk5HMgVOR04zNFIVIghFWENIQU5HRSoC\r\nTkcyBU5HTjM0\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1185544030478405632","_score":5.2289367,"_source":{"id":1185544030478405632,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGH6","enabled":true,"createdTimestampUtc":"2021-10-14T09:12:17.286Z","updatedTimestampUtc":"2022-04-17T05:48:49.130Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2026-03-25","lastTradingDate":"2026-02-25","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGH26"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGH6"}],"protobuf":"CICggJK7wPm5EBABGgNDTUUiAk5HKgROR0g2ShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDI2LTAzLTI1Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAyNi0wMi0yNUodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HSDI2UhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdINg==\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1185544029807316993","_score":5.2289367,"_source":{"id":1185544029807316993,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGV7","enabled":true,"createdTimestampUtc":"2021-10-14T09:12:17.126Z","updatedTimestampUtc":"2022-04-17T05:48:44.373Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2027-10-28","lastTradingDate":"2027-09-28","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGV27"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGV7"}],"protobuf":"CIGggNK4wPm5EBABGgNDTUUiAk5HKgROR1Y3ShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDI3LTEwLTI4Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAyNy0wOS0yOEodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HVjI3UhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdWNw==\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1153026101493108737","_score":5.2289367,"_source":{"id":1153026101493108737,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGK4","enabled":true,"createdTimestampUtc":"2021-07-16T15:37:38.862Z","updatedTimestampUtc":"2022-04-17T05:48:44.852Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2024-05-26","lastTradingDate":"2024-04-26","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGK4"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGK4"}],"protobuf":"CIGggOKV5JeAEBABGgNDTUUiAk5HKgROR0s0ShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDI0LTA1LTI2Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAyNC0wNC0yNkodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIOIgJUVCoCTkcyBE5HSzRSFCIIRVhDSEFOR0UqAk5H\r\nMgROR0s0\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1185544029245280256","_score":5.2289367,"_source":{"id":1185544029245280256,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGH1","enabled":true,"createdTimestampUtc":"2021-10-14T09:12:16.992Z","updatedTimestampUtc":"2022-04-17T05:48:39.077Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2031-03-26","lastTradingDate":"2031-02-26","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGH31"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGH1"}],"protobuf":"CICggMa2wPm5EBABGgNDTUUiAk5HKgROR0gxShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDMxLTAzLTI2Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAzMS0wMi0yNkodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HSDMxUhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdIMQ==\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1185544029245280257","_score":5.2289367,"_source":{"id":1185544029245280257,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGU0","enabled":true,"createdTimestampUtc":"2021-10-14T09:12:16.992Z","updatedTimestampUtc":"2022-04-17T05:48:39.564Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2030-09-28","lastTradingDate":"2030-08-28","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGU30"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGU0"}],"protobuf":"CIGggMa2wPm5EBABGgNDTUUiAk5HKgROR1UwShcaC3Byb2R1Y3RUeXBlIghPVVRSSUdIVEoTGgxj\r\ndXJyZW5jeUNvZGUiA1VTREosGgtkZXNjcmlwdGlvbiIdSGVucnkgSHViIE5hdHVyYWwgR2FzIEZ1\r\ndHVyZXNKDBoHZGl2aXNvciIBM0oNGgh0aWNrU2l6ZSIBMUoRGgl0aWNrVmFsdWUiBDEwLjBKFBoG\r\nZXhwaXJ5IgoyMDMwLTA5LTI4Sh0aD2xhc3RUcmFkaW5nRGF0ZSIKMjAzMC0wOC0yOEodGhRpc0Zy\r\nYWN0aW9uYWxDb250cmFjdCIFZmFsc2VKFhoNZGlzcGxheUZhY3RvciIFMC4wMDFKIRoKZXhjaGFu\r\nZ2VJZCITMjIyNDE3MTY1MTAwMjU0MDAzMlIPIgJUVCoCTkcyBU5HVTMwUhQiCEVYQ0hBTkdFKgJO\r\nRzIETkdVMA==\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1230533848052273153","_score":5.2289367,"_source":{"id":1230533848052273153,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGF33","enabled":true,"createdTimestampUtc":"2022-02-15T12:45:45.666Z","updatedTimestampUtc":"2022-04-17T05:48:30.908Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2033-01-29","lastTradingDate":"2032-12-29","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGF33"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGF33"}],"protobuf":"CIGggIq+gO+JERABGgNDTUUiAk5HKgVOR0YzM0oXGgtwcm9kdWN0VHlwZSIIT1VUUklHSFRKExoM\r\nY3VycmVuY3lDb2RlIgNVU0RKLBoLZGVzY3JpcHRpb24iHUhlbnJ5IEh1YiBOYXR1cmFsIEdhcyBG\r\ndXR1cmVzSgwaB2Rpdmlzb3IiATNKDRoIdGlja1NpemUiATFKERoJdGlja1ZhbHVlIgQxMC4wShQa\r\nBmV4cGlyeSIKMjAzMy0wMS0yOUodGg9sYXN0VHJhZGluZ0RhdGUiCjIwMzItMTItMjlKHRoUaXNG\r\ncmFjdGlvbmFsQ29udHJhY3QiBWZhbHNlShYaDWRpc3BsYXlGYWN0b3IiBTAuMDAxSiEaCmV4Y2hh\r\nbmdlSWQiEzIyMjQxNzE2NTEwMDI1NDAwMzJSDyICVFQqAk5HMgVOR0YzM1IVIghFWENIQU5HRSoC\r\nTkcyBU5HRjMz\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1230533848043884545","_score":5.2289367,"_source":{"id":1230533848043884545,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGZ33","enabled":true,"createdTimestampUtc":"2022-02-15T12:45:45.664Z","updatedTimestampUtc":"2022-04-17T05:48:29.489Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2033-12-28","lastTradingDate":"2033-11-28","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGZ33"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGZ33"}],"protobuf":"CIGggIa+gO+JERABGgNDTUUiAk5HKgVOR1ozM0oXGgtwcm9kdWN0VHlwZSIIT1VUUklHSFRKExoM\r\nY3VycmVuY3lDb2RlIgNVU0RKLBoLZGVzY3JpcHRpb24iHUhlbnJ5IEh1YiBOYXR1cmFsIEdhcyBG\r\ndXR1cmVzSgwaB2Rpdmlzb3IiATNKDRoIdGlja1NpemUiATFKERoJdGlja1ZhbHVlIgQxMC4wShQa\r\nBmV4cGlyeSIKMjAzMy0xMi0yOEodGg9sYXN0VHJhZGluZ0RhdGUiCjIwMzMtMTEtMjhKHRoUaXNG\r\ncmFjdGlvbmFsQ29udHJhY3QiBWZhbHNlShYaDWRpc3BsYXlGYWN0b3IiBTAuMDAxSiEaCmV4Y2hh\r\nbmdlSWQiEzIyMjQxNzE2NTEwMDI1NDAwMzJSDyICVFQqAk5HMgVOR1ozM1IVIghFWENIQU5HRSoC\r\nTkcyBU5HWjMz\r\n"}},{"_index":"securitylisting","_type":"securitylistingtype","_id":"1230533848048078848","_score":5.2289367,"_source":{"id":1230533848048078848,"assetType":"FUTURE","exchangeCode":"CME","securityCode":"NG","securityId":"NGN33","enabled":true,"createdTimestampUtc":"2022-02-15T12:45:45.665Z","updatedTimestampUtc":"2022-04-17T05:48:29.958Z","productType":"OUTRIGHT","currencyCode":"USD","description":"Henry Hub Natural Gas Futures","divisor":"3","tickSize":"1","tickValue":"10.0","expiry":"2033-07-28","lastTradingDate":"2033-06-28","isFractionalContract":"false","displayFactor":"0.001","exchangeId":"2224171651002540032","aliases":[{"securityIdSource":"TT","securityCode":"NG","securityId":"NGN33"},{"securityIdSource":"EXCHANGE","securityCode":"NG","securityId":"NGN33"}],"protobuf":"CICggIi+gO+JERABGgNDTUUiAk5HKgVOR04zM0oXGgtwcm9kdWN0VHlwZSIIT1VUUklHSFRKExoM\r\nY3VycmVuY3lDb2RlIgNVU0RKLBoLZGVzY3JpcHRpb24iHUhlbnJ5IEh1YiBOYXR1cmFsIEdhcyBG\r\ndXR1cmVzSgwaB2Rpdmlzb3IiATNKDRoIdGlja1NpemUiATFKERoJdGlja1ZhbHVlIgQxMC4wShQa\r\nBmV4cGlyeSIKMjAzMy0wNy0yOEodGg9sYXN0VHJhZGluZ0RhdGUiCjIwMzMtMDYtMjhKHRoUaXNG\r\ncmFjdGlvbmFsQ29udHJhY3QiBWZhbHNlShYaDWRpc3BsYXlGYWN0b3IiBTAuMDAxSiEaCmV4Y2hh\r\nbmdlSWQiEzIyMjQxNzE2NTEwMDI1NDAwMzJSDyICVFQqAk5HMgVOR04zM1IVIghFWENIQU5HRSoC\r\nTkcyBU5HTjMz\r\n"}}]}} '

# Convert string to python dict

product_dict = json.loads(str(product)

product_dict['hits']['hits'][0]['_source']['securityId']
Traceback (most recent call last):
  File ".prog.py", line 9, in <module>
  File "usr/lib/python3.8/json/_init_.py", line 357, in loads
    return _default_decoder.decode(s)
  File "usr/lib/python3.8/json/decode.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "usr/lib/python3.8/json/decode.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 1000 (char999)

根据错误消息,您需要按照我在评论中所说的那样清理 json

您可能会从 json 的来源获得一些帮助,但如果没有,可能只需将所有控制字符替换为空字符串并获得一些成功,但让自己对未来保持开放来源问题。

所以现在您可以使用:

product = product.replace('\n', '').replace('\r', '')

然后它变成了一个索引问题。