Parsing a complex JSON object with Python: search a specific key/value pair
Question: **General question** : how can I search a specific `key:value` pair in a JSON
using Python?

**Details for the specific case** : I'm reading ~ 45'000 JSON objects, each
one of them look [like this
one](http://opac.sbn.it/opacmobilegw/search.json?any=Per%20la%20inaugurazione%20dell%27Asilo%20infantile%20Strozzi%20nei%20locali%20della%20caserma%20Filippini%20gi%C3%A0%20convento%20della%20Vittoria%20/%20parole%20di%20mons.%20Carlo%20Savoia&type=0&start=0&rows=3).  
As you can see, inside every JSON there are several dictionaries that have the
same keys (but different values): `"facetName`, `"facetLabel"`,
`"facetValues"`.  
I'm interested in the dictionary that starts with `"facetName": "soggettof"`,
that goes like:

    
    
    {
      "facetName": "soggettof",
      "facetLabel": "Soggetto",
      "facetValues": [
        [
          "chiesa - storia - documenti",
          "chiesa - storia - documenti",
          "1"
        ],
        [
          "espiazione - mare mediterraneo <bacino> - antichita - congressi - munster - 1999",
          "espiazione - mare mediterraneo <bacino> - antichita - congressi - munster - 1999",
          "1"
        ],
        [
          "lega rossa combattenti - storia",
          "lega rossa combattenti - storia",
          "1"
        ],
        [
          "pavia - storia ecclesiastica - origini-sec. 12.",
          "pavia - storia ecclesiastica - origini-sec. 12.",
          "1"
        ],
        [
          "pavia <diocesi> - storia - origini-sec. 12.",
          "pavia <diocesi> - storia - origini-sec. 12.",
          "1"
        ],
        [
          "persia - sviluppo economico - 1850-1900 - fonti diplomatiche inglesi",
          "persia - sviluppo economico - 1850-1900 - fonti diplomatiche inglesi",
          "1"
        ]
    

Please note, that not all the JSON objects have that.

How can I grab the values of the `facetValues` list, but only in the
dictionary that I'm interested in?


Answer: I found your question a little confusing, partially because the data shown in
it was _not_ really the JSON-object you needed to extract the information from
-- but was instead an example of a sub-JSON-object you wanted to extract it
from. Fortunately you had a
[link](http://opac.sbn.it/opacmobilegw/search.json?any=Per%20la%20inaugurazione%20dell%27Asilo%20infantile%20Strozzi%20nei%20locali%20della%20caserma%20Filippini%20gi%C3%A0%20convento%20della%20Vittoria%20/%20parole%20di%20mons.%20Carlo%20Savoia&type=0&start=0&rows=3)
to the outer-most container JSON-object (even though the data in corresponding
sub-JSON-object in it was different). This is the link data:

    
    
    json_obj = {"numFound":1,"start":0,"rows":3,"briefRecords":[{"progressivoId":0,"codiceIdentificativo":"IT\\ICCU\\LO1\\0120590","autorePrincipale":"Savoia, Carlo","titolo":"Per la inaugurazione dell'Asilo infantile Strozzi nei locali della caserma Filippini già convento della Vittoria / parole di mons. Carlo Savoia","pubblicazione":"Mantova : Tip. Eredi Segna, 1870","livello":"Monografia","tipo":"Testo a stampa","numeri":[],"note":[],"nomi":[],"luogoNormalizzato":[],"localizzazioni":[],"citazioni":[]}],"facetRecords":[{"facetName":"level","facetLabel":"Livello bibliografico","facetValues":[["Monografia","m","1"]]},{"facetName":"tiporec","facetLabel":"Tipo di documento","facetValues":[["Testo a stampa","a","1"]]},{"facetName":"nomef","facetLabel":"Autore","facetValues":[["savoia, carlo","savoia, carlo","1"]]},{"facetName":"soggettof","facetLabel":"Soggetto","facetValues":[["mantova - asili infantili","mantova - asili infantili","1"]]},{"facetName":"luogof","facetLabel":"Luogo di pubblicazione","facetValues":[["mantova","mantova","1"]]},{"facetName":"lingua","facetLabel":"Lingua","facetValues":[["italiano","ita","1"]]},{"facetName":"paese","facetLabel":"Paese","facetValues":[["italia","it","1"]]}]}
    

It's important to have this outer-most container because it is through it you
will have to begin drilling-down to the portion you want. Once you have the
actaul data you may need to reformat it to make its structure clear. You can
do this by hand, or have the computer do it with `print(json.dumps(json_obj,
indent=2))` although the results from that can sometimes have a little too
much white space in them (which can be counterproductive).

That being the case here, below is the more succinct version I came up doing
it manually that still let's me see the overall layout of the data:

    
    
    json_obj = {"numFound" : 1,
                 "start" : 0,
                 "rows" : 3,
                 "briefRecords" : [
                    {"progressivoId" : 0,
                     "codiceIdentificativo" : "IT\\ICCU\\LO1\\0120590",
                     "autorePrincipale" : "Savoia, Carlo",
                     "titolo" : "Per la inaugurazione dell'Asilo infantile Strozzi nei locali della caserma Filippini già convento della Vittoria / parole di mons. Carlo Savoia",
                     "pubblicazione" : "Mantova : Tip. Eredi Segna, 1870",
                     "livello" : "Monografia",
                     "tipo" : "Testo a stampa",
                     "numeri" : [],
                     "note" : [],
                     "nomi" : [],
                     "luogoNormalizzato" : [],
                     "localizzazioni" : [],
                     "citazioni" : []
                    }
                 ],
                 "facetRecords" : [
                    {"facetName" : "level" ,
                     "facetLabel" : "Livello bibliografico" ,
                     "facetValues" : [["Monografia" , "m" , "1"]]},
                    {"facetName" : "tiporec" ,
                     "facetLabel" : "Tipo di documento" ,
                     "facetValues" : [["Testo a stampa" , "a" , "1"]]},
                    {"facetName" : "nomef" ,
                     "facetLabel" : "Autore" ,
                     "facetValues" : [["savoia, carlo" , "savoia, carlo" , "1"]]},
                    {"facetName" : "soggettof" ,
                     "facetLabel" : "Soggetto" ,
                     "facetValues" : [["mantova - asili infantili" , "mantova - asili infantili" , "1"]]},
                    {"facetName" : "luogof" ,
                     "facetLabel" : "Luogo di pubblicazione" ,
                     "facetValues" : [["mantova" , "mantova" , "1"]]},
                    {"facetName" : "lingua" ,
                     "facetLabel" : "Lingua" ,
                     "facetValues" : [["italiano" , "ita" , "1"]]},
                    {"facetName" : "paese" ,
                     "facetLabel" : "Paese" ,
                     "facetValues" : [["italia" , "it" , "1"]]}
                 ]
                }
    

Once you have something like this, it usually makes it fairly easy to
determine what code is needed. In this case it's:

    
    
    target_facet_name = "soggettof"
    
    for record in json_obj["facetRecords"]:
        if record["facetName"] == target_facet_name:
            for value in record["facetValues"]:
                print value
    

Since `facetRecords` is a list, a linear search through them is required to
find the one(s) desired.

