To be honest, I don’t know what exactly to call it, let’s just call it ‘projection’, but it is very powerful and useful.
First of all, LET me explain my understanding:
- The wildcard
*
- The wildcard generates a list
- Everything that follows the wildcard goes into this list
C. List and Slice
The application of tables and slices in circulation.
1. Return common interface data
Each element is a JSON object with the same key-value in it. If you want to retrieve all values of a key, how do you do that?
import jmespath dic_1 = { "people": [ {"first": "James", "last": "d"}, {"first": "Jacob", "last": "e"}, {"first": "Jayden", "last": "f"}, {"missing": "different"} ], "foo": {"bar": "baz"} } path = jmespath.search("people[*]", Dic_1) print(path) # D:\Daily\whatisyeild>python jmespath_demo.py [{'first': 'James', 'last': 'D '}, {'first': 'Jacob', 'last': 'e'}, {'first': 'Jayden', 'last': 'f'}, {'missing': 'different'}]Copy the code
You can see that the wildcard * in people[*] matches all the elements in the list and is returned as a list. People [*].first
import jmespath dic_1 = { "people": [ {"first": "James", "last": "d"}, {"first": "Jacob", "last": "e"}, {"first": "Jayden", "last": "f"}, {"missing": "different"} ], "foo": {"bar": "baz"} } path = jmespath.search("people[*].first", D:\Daily\whatisyeild>python jmespath_demo.py ['James', 'Jacob', 'Jayden']Copy the code
As you can see, the elements found are also placed in the list created by the wildcard and returned. Also, you can slice:
import jmespath dic_1 = { "people": [ {"first": "James", "last": "d"}, {"first": "Jacob", "last": "e"}, {"first": "Jayden", "last": "f"}, {"missing": "different"} ], "foo": {"bar": "baz"} } path = jmespath.search("people[:2].first", D:\Daily\whatisyeild>python jmespath_demo.py ['James', 'Jacob']Copy the code
2. More complex data return
For example, nested lists in dictionary values can still be projected into lists created by wildcards.
import jmespath dic_1 = { "people": [ {"first": "James", "last": "d"}, {"first": "Jacob", "last": "e"}, {"first": "Jayden", "last" : "f"}, {" first ": [[" a", "b", "c"], 2 5-tetrafluorobenzoic], "last" : "g"}, {" missing ":" the company "}], "foo" : {" bar ": "baz"} } path = jmespath.search("people[*].first[0]", D:\Daily\whatisyeild>python jmespath_demo.py [['a', 'b', 'c']]Copy the code
3. Invalidity
Note that if the element to the right of the wildcard is null, it will be ignored when projected into the result list. For example, people[*].first[0][10] is out of bounds, so the output is an empty list [].
import jmespath dic_1 = { "people": [ {"first": "James", "last": "d"}, {"first": "Jacob", "last": "e"}, {"first": "Jayden", "last" : "f"}, {" first ": [[" a", "b", "c"], 2 5-tetrafluorobenzoic], "last" : "g"}, {" missing ":" the company "}], "foo" : {" bar ": Search ("people[*]. First [0][10]", dic_1) print(path) # D:\Daily\whatisyeild>python jmespath_demo.py []Copy the code
Also, list projections are only valid for list lists. If the value is not a list, the result of the expression is NULL. For example, a list projection of foo[*] would not work, because “foo”: {“bar”: “baz”} corresponds to a JSON object, so it would be null.
import jmespath dic_1 = { "people": [ {"first": "James", "last": "d"}, {"first": "Jacob", "last": "e"}, {"first": "Jayden", "last" : "f"}, {" first ": [[" a", "b", "c"], 2 5-tetrafluorobenzoic], "last" : "g"}, {" missing ":" the company "}], "foo" : {" bar ": Search ("foo[*]", dic_1) print(path) # D:\Daily\whatisyeild>python jmespath_demo.py NoneCopy the code