0

I have tho following json file:

    {
      "layout": {
          "user": {
              "pages": {
                  "Home": { // first page
                        "sub_domain_name": "demo",
                        "title": "Home",
                        "page_id": 111111,
                       "desktop": [ // first node in first page
                              {
                                "grid_name": "row",
                                "component_id": "31fac419-f1ff-4614",
                                "main_classes": "border border-primary",
                               "items": [ // first node in desktop
                                         {
                                           "grid_name": "col",
                                            "component_id": "111111",
                                            "component_name": "Card",
                                            "items": []
                                          }
                                      ]
                                   },
                            {
                             "grid_name": "row",
                             "component_id": "222222",
                             "main_classes": "border-top-0",
                              "items": [
                                         {
                                          "grid_name": "col",
                                          "component_id": "3333",
                                          "component_name": "Author",
                                          "items": []
                                       }
                                                    
                                     ]
                                 },
                                            
                          ]
                     }
                                        
     "section": [
                  "s11"
                ],
    "mobile": [ //second node in first page
               {
                 "grid_name": "row",
                 "component_id": "4444",
                 "main_classes": "border",
                 "items": [ // first node in mobile
                     {
                       "grid_name": "col",
                      "component_id": "5555",
                       "component_name": "Card",
                        "items": []
                      }
                  ]
            },
      {
      "grid_name": "row",
      "component_id": "888",
      "main_classes": "border-top-0 ",
      "items": [
               "grid_name": "col",
               "component_id": "999"
               "component_name": "Card",                                                                                                 
               "items": []
              },
                                                    
             {
              "grid_name": "col",
               "component_id": "11",
             "component_name": "Media",                                                                                        
             "items": []
             }
       ]
    }
                                            
      ]
     }
     ]
     },
      "contact": { // second page
                 "sub_domain_name": "demo3",
                  "title": "contact",
                 "page_id": 2222222,
            "desktop": [ // first node in second page
                        {
                        "grid_name": "row",
                         "component_id": "22",
                          "component_name": "Table",
                         "items": []
                         },
                        {
                          "grid_name": "row",
                          "component_id": "99",
                          "component_name": "Table",
                                                
                         }
                                            
                     }
              ],
     "section": [
                 "s4"                           
                 ],    
      
     "mobile": [ // second node in second page
            {
              "grid_name": "row",
              "component_id": "67",
              "component_name": "Component",
               "items": []
     },                                       
     {                                       
      "grid_name": "row",                                         
       "component_id": "0000",                                        
       "component_name": "Table",               
     }                                       
     ]                                        
    }                 

I want to print all component_name from this json file using python. Not shure what the problem is tho. Does anyone see the problem?

import json
myJsonFile=open("config-2_new.json")
jsondata=myJsonFile.read()
obj=json.loads(jsondata)
dict_process=obj['layout']
for i in dict_process:
    inner_page=dict_process[i]['pages']
    pages_list=inner_page.keys()
    for j in pages_list:
        desktop=inner_page[j]['desktop']
        for x in range(0,len(desktop)):
            child_keys=desktop[x]['child']
            for k in child_keys:
                print (k['component_name']) 

Thanks for any answers allready.

  • Please provide at least what your intended output is, and if you ( and what) an error-code got. – kaliiiiiiiii Jan 20 '23 at 07:10
  • i am not getting error ...but don't getting the component name outside of child node from another page(contact) – Pooja Patil Jan 20 '23 at 07:16
  • Your json seems invalid to me? – kaliiiiiiiii Jan 20 '23 at 07:29
  • it doesn't look like valid json, but if you manage to parse it into `obj` as a python dictionary, then I have [this set of functions](https://pastebin.com/NRxKRJPn) that you could use like `getNestedVal(obj, 'component_name', 'just_val_all')` to get a list of all values with the key `component_name` in `obj` – Driftr95 Jan 20 '23 at 07:50
  • i am not getting ans – Pooja Patil Jan 20 '23 at 09:32

1 Answers1

0

Your json is invalid. For parsing a specific key use jsonpath-ng module. Also please refer jsonpath_rw for Python - parsing a specific key

I tried with removing some part of json and then parsing, you can use below. I have stored your json string in json_data variable.

jsonpath_ng.parse("layout.user.pages.Home.desktop[*].items.component_name").find(json_data)