Extracting json data using jmespath third-party module

Posted by jhbalaji on Thu, 14 Nov 2019 19:05:07 +0100

In the work, it is often necessary to find the value of a key in json. If the json level is too deep, it is troublesome to use the dictionary's own get method.
Here is a demonstration of the third-party module jmespath extracting json keys and values.

pip install jmespath

I. Basic operation

Query the value corresponding to the key

import jmespath

source = {"a": "foo", "b": "bar", "c": "baz"}
result = jmespath.search('b', source)
print(repr(result))

Result: 'bar'

Use. Operator

source1 = {"a": {"b": {"c": {"d": "value"}}}}
result1 = jmespath.search('a.b.c', source1)
print(repr(result1))

Results: {'d': 'value'}

Subscript operation, array only

source_2 = ["a", "b", "c", "d", "e", "f"]
index_result = jmespath.search("[1]",source_2)
print(repr(index_result))

Results: 'b'

Mixed operation of subscript and. Operator

source3 = {"a": {
  "b": {
    "c": [
      {"d": [0, [1, 2]]},
      {"d": [3, 4]}
    ]
  }
}}
result3 = jmespath.search('a.b.c[0].d[1][0]',source3)
print(repr(result3))
//Results: 1

Two, slice

source_4 = ["a", "b", "c", "d", "e", "f"]
result4 = jmespath.search("[1:3]",source_4)
print(repr(result4))

Results: ['b ',' c ']

Two. Projection

Projection is to define the format at the beginning, and then take the value according to the format.
Projection mainly includes the following:

  • List projects
  • Slice projects
  • Object Projections
  • Flatten projects regular projection
  • Filter projects

Note: use [] for list and use [] for dictionary

# List and Slice Projections
source5 = {
  "people": [
    {"first": "James", "last": "d"},
    {"first": "Jacob", "last": "e"},
    {"first": "Jayden", "last": "f"},
    {"missing": "different"}
  ],
  "foo": {"bar": "baz"}
}
result5 = jmespath.search('people[*].first', source5)
print(result5)

Results: ['James', 'Jacob', 'Jayden']

# Object Projections
# Whereas a list projection is defined for a JSON array, an object projection is defined for a JSON object. You can create an object projection using 
# the * syntax.
source6 = {
  "ops": {
    "functionA": {"numArgs": 2},
    "functionB": {"numArgs": 3},
    "functionC": {"variadic": True}
  }
}
result6 = jmespath.search('ops.*.numArgs', source6)
print(repr(result6))

Results: [3, 2]

# Filter projects with filter conditions
# Format [? < expression > < comparator > < expression >]
# Support = =,! =, < and < =, > and >=
# In this example we're searching through the people array. Each element in this array contains a hash of two elements, and each value in the hash
# is itself a hash. We're trying to retrieve the value of the general key that contains an age key with a value above 20.
source7 = {
  "people": [
    {
      "general": {
        "id": 100,
        "age": 20,
        "other": "foo",
        "name": "Bob"
      },
      "history": {
        "first_login": "2014-01-01",
        "last_login": "2014-01-02"
      }
    },
    {
      "general": {
        "id": 101,
        "age": 30,
        "other": "bar",
        "name": "Bill"
      },
      "history": {
        "first_login": "2014-05-01",
        "last_login": "2014-05-02"
      }
    }
  ]
}
result7 = jmespath.search("people[?general.age > `20`].general | [0]", source7)
print(repr(result7))

{'id': 101, 'other': 'bar', 'age': 30, 'name': 'Bill'}

For more usage of jmspath, please refer to:

jmespath official API

Topics: JSON pip