简体   繁体   中英

Parsing Maxmind JSON with jq (or python3)?

I have been experimenting with jq but cannot parse out the iso_code value in the output from the Maxmind beta application mmdbinspect which is JSON output.

I get a 'Cannot index array with string' error message no matter what I tried. ie. jq -r .'Database'

{
      "Database": "/usr/local/var/GeoIP/GeoLite2-City.mmdb",
      "Records": [
        {
          "Network": "8.8.8.8/17",
          "Record": {
            "continent": {
              "code": "NA",
              "geoname_id": 6255149,
              "names": {
                "de": "Nordamerika",
                "en": "North America",
                "es": "Norteamérica",
                "fr": "Amérique du Nord",
                "ja": "北アメリカ",
                "pt-BR": "América do Norte",
                "ru": "Северная Америка",
                "zh-CN": "北美洲"
              }
            },
            "country": {
              "geoname_id": 6252001,
              "iso_code": "US",
              "names": {
                "de": "USA",
                "en": "United States",
                "es": "Estados Unidos",
                "fr": "États-Unis",
                "ja": "アメリカ合衆国",
                "pt-BR": "Estados Unidos",
                "ru": "США",
                "zh-CN": "美国"
              }
            },
            "location": {
              "accuracy_radius": 1000,
              "latitude": 37.751,
              "longitude": -97.822,
              "time_zone": "America/Chicago"
            },
            "registered_country": {
              "geoname_id": 6252001,
              "iso_code": "US",
              "names": {
                "de": "USA",
                "en": "United States",
                "es": "Estados Unidos",
                "fr": "États-Unis",
                "ja": "アメリカ合衆国",
                "pt-BR": "Estados Unidos",
                "ru": "США",
                "zh-CN": "美国"
              }
            }
          }
        }
      ],
      "Lookup": "8.8.8.8"
    }

Referring this this post , do I need to flatting out the JSON output first?

Maybe Python3 instead of using jq which be easier...

From the jq FAQ:

The encoding of the file might not be valid for JSON text, which is defined as a sequence of Unicode code points. jq currently requires the text be encoded as UTF-8 (and therefore allows ASCII). Note that the default encoding used in PowerShell when redirecting terminal output to a file is UTF-16. If you need to convert from one encoding to another, consider using iconv, or if you are using Windows, try pasting your JSON into Notepad and saving the file as a UTF-8 file.

Once you've sorted out the encoding issues, you might want to consider:

.. | .iso_code? // empty

or if you just want the distinct answers:

[.. | .iso_code? // empty] | unique[]

See also https://jqplay.org/s/mv9aO0DTqd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM