A
A
Andrew2021-12-01 20:46:33
Python
Andrew, 2021-12-01 20:46:33

How to find and take the necessary data from json?

There is a group of files with the following content:

{
  "responseHeader": {
    "zkConnected": true,
    "status": 0,
    "QTime": 394,
    "params": {
      "q": "+2019 +BUICK +ENCLAVE",
      "defType": "edismax",
      "qf": "title_en^200.0 title_es^200 title_ar^200 title_pt^200 title_ru^200 title_th^200 title_fr^200 title_ko^200 category_translated^150.0 category_translated_path^120.0 category_key_path^90.0 mmy^80.0 category_key^75",
      "fl": "imdocid,imcontent_text_id,path,title_ordered,category_key,file_type",
      "fq": [
        "channel:MANUALS",
        "file_type:application/pdf",
        " -(usergroup_key:[* TO *] OR -usergroup_key:PUBLIC)",
        " -(view_key:[* TO *] OR -view_key:PUBLIC)"
      ],
      "rows": "15",
      "_forwardedCount": "1",
      "stopwords": "true",
      "useParams": "default"
    }
  },
  "response": {
    "numFound": 3,
    "start": 0,
    "numFoundExact": true,
    "docs": [
      {
        "imdocid": "MA4840",
        "path": "/resources/sites/GMA/content/staging/MANUALS/4000/MA4840/en_US/7.0/GTK_2019_Buick_Enclave_84149417_A.pdf",
        "file_type": "application/pdf",
        "category_key": [
          "QUICK_REFERENCE_MANUALS_BROWSE",
          "BUICK",
          "ENCLAVE",
          "2019",
          "BUICK_INTELLILINK"
        ],
        "imcontent_text_id": "863d13356ad04f409b0d2b36061e63b1",
        "title_ordered": "Getting to Know Your 2019 Buick Enclave"
      },
      {
        "imdocid": "MA4284",
        "path": "/resources/sites/GMA/content/staging/MANUALS/4000/MA4284/en_US/2.0/19_BUI_Enclave_OM_en_US_U_84149412B_2018SEP12_2P.pdf",
        "file_type": "application/pdf",
        "category_key": [
          "OWNERS_MANUALS_BROWSE",
          "BUICK",
          "ENCLAVE",
          "2019"
        ],
        "imcontent_text_id": "b0640df8bc194096b65babf09543de11",
        "title_ordered": "2019 Buick Enclave Owner Manual"
      },
      {
        "imdocid": "MA4119",
        "path": "/resources/sites/GMA/content/staging/MANUALS/4000/MA4119/en_US/2.0/19_BUI_IM_en_US_U_84134605B_2018JUL03_2P.pdf",
        "file_type": "application/pdf",
        "category_key": [
          "INFOTAINMENT_MANUALS_BROWSE",
          "BUICK",
          "ENCLAVE",
          "REGAL",
          "ENVISION",
          "ENCORE",
          "2019",
          "BUICK_INTELLILINK"
        ],
        "imcontent_text_id": "09139b09b5004fe78bf59b574a8cb096",
        "title_ordered": "2019 Buick Infotainment System Manual"
      }
    ]
  }
}


I need to "pull out" the values ​​of the fields (if they are, if they are not then skip):
['docs'][6]['path'] and ['docs'][6]['path']['category_key' ][0]
to be more precise, then check if the value of the category_key key contains the phrase OWNERS_MANUALS* then take the values ​​of the category_key key and the value of the path key. At the same time, nesting can be different and OWNERS_MANUALS can be not one but 2,4,6 or not at all.
Help, I've already broken my "head", some monsters are obtained, but I feel there should be an elegant solution)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Kuts, 2021-12-01
@fox_12

A classic example for solving by recursion.
Recurse through the entire structure, iterating over the keys or elements of the list. If you meet a category_key at the same nesting level, then remember the path from the same nesting level, if you meet an element of the dict or list type, then you call yourself with the transfer of this element.
Well, select the desired category_key in the list of which will be OWNERS_MANUALS

S
Sergey Pankov, 2021-12-01
@trapwalker

What does different nesting mean?
Show what monsters are obtained and what exactly is the problem? The task is trivial: to loop through the list, check the condition and return the results through yeld, or combine them into a collection.

S
sivabur, 2015-05-10
@sivabur

It’s worth it if there is less code and it will be clearer, the current is some kind of refactoring and not optimization)

S
SMARTi, 2015-05-10
@SMARTi

If there is duplicate code, it should almost always be replaced with a single generic method. But it all depends on the situation and implementation. Also, for readability, it would not be bad to replace 2 = right, 1 = left with some Enum or constants.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question