Python equivalent of Wolfram Language Query for JSON?

The Wolfram Language has a Query function that can traverse data structures and apply functions at different levels of the structure. I am working with multi-level JSON structures and need a function that has similar functionality as that of Query in the Wolfram Language.

Which Python package and function(s) best replicates this?

For a minimal working example, say I have the following JSON structure. (String escapes omitted for simplicity)

x = {
    "Dims1":[
        {
            "Apple":{
                "Baking":[
                    "Pie",
                    "Tart"
                ],
                "Plant":"Tree",
                "Tons":{
                    "2017":1.23e1,
                    "2018":1.12e1
                }
            }
        },
        {
            "Tomato":{
                "Cooking":[
                    "Stew",
                    "Sauce"
                ],
                "Plant":"Vine",
                "Tons":{
                    "2017":8.1,
                    "2018":8.3
                }
            }
        },
        {
            "Banana":{
                "Name":"Banana",
                "Baking":[
                    "Bread"
                ],
                "Cooking":[
                    "Fried"
                ],
                "Plant":"Arborescent",
                "Tons":{
                    "2017":0.8,
                    "2018":0.5
                }
            }
        }
    ],
    "Dims2":[
        {
            "Apple":{
                "Name":"Apple",
                "Baking":[
                    "Pie",
                    "Tart"
                ],
                "Plant":"Tree",
                "Tons":{
                    "2017":1.31e1,
                    "2018":1.01e1
                }
            }
        },
        {
            "Sweet Potato":{
                "Cooking":[
                    "Fried",
                    "Steamed"
                ],
                "Baking":[
                    "Pie"
                ],
                "Plant":"Vine",
                "Tons":{
                    "2017":1.11e1,
                    "2018":1.91e1
                }
            }
        }
    ]
}

In Wolfram Language I can

a = GeneralUtilities`ToAssociations@ImportString[x, "JSON"]
|
 "Dims1" -
  {
   |"Apple" -
     |"Baking" - {"Pie", "Tart"}, "Plant" - "Tree",
      "Tons" - |"2017" - 12.3, "2018" - 11.2||
    |,
   |"Tomato" - 
     |"Cooking" - {"Stew", "Sauce"}, "Plant" - "Vine",
      "Tons" - |"2017" - 8.1, "2018" - 8.3||
    |,
   |"Banana" -
     |"Name" - "Banana", "Baking" - {"Bread"}, 
      "Cooking" - {"Fried"}, "Plant" - "Arborescent",
      "Tons" - |"2017" - 0.8, "2018" - 0.5||
    |
   },
 "Dims2" -
  {
   |"Apple" -
     |"Name" - "Apple", "Baking" - {"Pie", "Tart"}, 
      "Plant" - "Tree",
      "Tons" - |"2017" - 13.1, "2018" - 10.1||
    |,
   |"Sweet Potato" -
     |"Cooking" - {"Fried", "Steamed"}, "Baking" - {"Pie"}, 
      "Plant" - "Vine",
      "Tons" - |"2017" - 11.1, "2018" - 19.1||
    |
   }
 |

and then with Query

Query[All, All, All, {"Baking"}]@a
|"Dims1" - 
   {|"Apple" - |"Baking" - {"Pie", "Tart"}||, 
    |"Tomato" - |"Baking" - Missing["KeyAbsent", "Baking"]||, 
    |"Banana" - |"Baking" - {"Bread"}||}, 
  "Dims2" - 
   {|"Apple" - |"Baking" - {"Pie", "Tart"}||, 
    |"Sweet Potato" - |"Baking" - {"Pie"}||}
|

and include functions such as

Query[All, Join /* Flatten /* DeleteDuplicates, Values, "Baking" /* DeleteMissing]@a
|"Dims1" - {"Pie", "Tart", "Bread"}, "Dims2" - {"Pie", "Tart"}|

and

Query[All, Merge[Total] /* DateListPlot, All, "Tons", 
  KeyMap[DateObject[{FromDigits@#}, "Year"] ]]@a

How is this done with JSON in Python?

Topic wolfram-language json data python

Category Data Science


ObjectPath is a query language for semi-structured data, including JSON, and has a Python API.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.