Python Forum
Read nested data from JSON - Getting an error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read nested data from JSON - Getting an error
#6
(Nov-23-2022, 03:24 AM)deanhystad Wrote: It is just a list of dictionaries inside a dictionary inside a dictionary inside a .... Wow, that is a bad looking data structure.
That's how data structure are many times coming from largere API resonse in Json format,i would not say it's bad structure.
The data comes for AWS CloudFormation
(Nov-23-2022, 02:31 AM)mrapple2020 Wrote: .
I ran the code below and it executes. Can you do me a favor and point out from here how can I retrieve the value of the "compliance" key?
The RAW_DATA may already been encoded for use in Python,how are you getting the data?
Also for output to work on site DeaD_EyE postet have to this like this to get double quotation marks.
Or as mention just the RAW_DATA that's in string could be used if as it's alradt a dictionary put in a string.
import json
from nested_lookup import nested_lookup

def new_func():
    RAW_DATA =  """
    {"131998795651": {
            "cloudformation": {
                "CloudFormation Drift Detection Analysis": {
                    "analysis_results": [{
                        "account_id": "131998795651",
                        "analysis_scope": "Service",
                        "analyzer_name": "CloudFormation Drift Detection Analysis",
                        "compliance": "Pass",
                        "evidence": "Cloudformation stack is in sync for drift detection."
    }]}}}}"""

    return RAW_DATA

RAW_DATA = new_func()
data = json.loads(RAW_DATA)
js_data = json.dumps(data, indent=2)
print(js_data)
Now can copy data into Json Crack
Output:
{ "131998795651": { "cloudformation": { "CloudFormation Drift Detection Analysis": { "analysis_results": [ { "account_id": "131998795651", "analysis_scope": "Service", "analyzer_name": "CloudFormation Drift Detection Analysis", "compliance": "Pass", "evidence": "Cloudformation stack is in sync for drift detection." } ] } } } }
See also that i have imported nested_lookup,this make it easy to search direclity in a nested stucture like this.
>>> nested_lookup("compliance", data)
['Pass']
So the same result as deanhystad break down into the dictionary.
>>> data["131998795651"]["cloudformation"]["CloudFormation Drift Detection Analysis"]["analysis_results"][0]["compliance"]
'Pass'
Reply


Messages In This Thread
RE: Read nested data from JSON - Getting an error - by snippsat - Nov-23-2022, 03:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 273 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 453 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  json loads throwing error mpsameer 8 758 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  [split] Parse Nested JSON String in Python mmm07 4 1,588 Mar-28-2023, 06:07 PM
Last Post: snippsat
  json decoding error deneme2 10 3,818 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 2,029 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  python requests library .JSON() error mHosseinDS86 6 3,507 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Reading Data from JSON tpolim008 2 1,128 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Read JSON via API and write to SQL database TecInfo 5 2,271 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Read data via bluetooth frohr 9 3,499 Jul-10-2022, 09:51 AM
Last Post: frohr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020