0

How to convert the following json string to a Python object?

{                                                                                                                                                                                               
    "action": "create",                                                                                                                                                                         
    "data": {                                                                                                                                                                                   
        "geometry": {                                                                                                                                                                           
            "coordinates": [                                                                                                                                                                    
                -95.12,                                                                                                                                                                         
                16.52,                                                                                                                                                                          
                -52.0                                                                                                                                                                           
            ],                                                                                                                                                                                  
            "type": "Point"                                                                                                                                                                     
        },                                                                                                                                                                                      
        "id": "20180303_0000046",                                                                                                                                                               
        "properties": {                                                                                                                                                                         
            "auth": "UNM",                                                                                                                                                                      
            "depth": [{
                "pacific": {
                    "value":52.0
                }
            }],                                                                                                                                                                      
            "evtype": "ke",                                                                                                                                                                     
            "flynn_region": "OAXACA, MEXICO",                                                                                                                                                   
            "lastupdate": "2018-03-03T10:26:00.0Z",                                                                                                                                             
            "lat": 16.52,                                                                                                                                                                       
            "lon": -95.12,                                                                                                                                                                      
            "mag": 4.0,                                                                                                                                                                         
            "magtype": "m",                                                                                                                                                                     
            "source_catalog": "EMSC-RTS",                                                                                                                                                       
            "source_id": "652127",                                                                                                                                                              
            "time": "2018-03-03T07:09:05.0Z",                                                                                                                                                   
            "unid": "20180303_0000046"                                                                                                                                                          
        },                                                                                                                                                                                      
        "type": "Feature"                                                                                                                                                                       
    }                                                                                                                                                                                           
}  

I want to read quake.data.properties.depth[0].pacific

I have used the following ref as example, but that code could not read "pacific" attribute.

ref: Creating a Python object from a Json string

  • This seems unclear what you need. Is this a string or a dict? If it's a string, are you trying to get a dict? If you want one object to represent this entire structure, you'll need to write a class. If you just want the one property, `json.loads(s)['data']['properties']['depth'][0]['pacific']`? – ggorlen Apr 07 '20 at 18:29
  • @Božo Stojković – user1625342 Apr 07 '20 at 18:29
  • There's no such thing as a "JSON dict". There are JSON objects, which are particular string values that can be decoded into a Python `dict`. If you already have the `dict`, and are asking how to use that to initialize an instance of some other Python class, this question has nothing to do with JSON. – chepner Apr 07 '20 at 18:31
  • @ggorlen - my apologies for not being clear. I want to convert the json string to an object and read it this way quake.data.properties.depth[0].pacific rather than reading it from a dict like you mentioned. – user1625342 Apr 07 '20 at 18:52
  • I see. You'll need a class, then. There are third party tools that let you do this, see [this thread](https://stackoverflow.com/questions/12465588/convert-a-json-schema-to-a-python-class). – ggorlen Apr 07 '20 at 18:59

0 Answers0