In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how Python uses the jmespath module to deal with json data. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Since it is a third-party library, it must be installed. First install the jmespath library by pip.
Pip install jmespath
Import the installed module into the code block.
Import jmespath as jp
There is a very important and convenient function in jmespath that is search, which can be found for you no matter how abnormal your json data is. The boss who wrote this frame also took great pains.
I first prepared the simplest json data structure with a data level of 1.
Json_data1 = {"name": "Python concentration camp", "age": "10 years"} res = jp.search ("name", json_data1) print (res) # Python
If it is multi-level json data, you can use key1.key2.key3 to get the value value.
Json_data2 = {"names": {"name": "Python camp", "age": "5 years"}} res = jp.search ("names.name", json_data2) print (res) # Python concentration camp
Since it's for json data processing, it certainly supports array lookups as well. For the search of json data in array form, the value of data is mainly obtained by the way of array subscript.
Json_data3 = ['Python camp', 'Sir.wang'] res = jp.search ("[0]", json_data3) print (res) # Python concentration camp
If the json data is composed of arrays and dictionaries, you can also use a combination of lookups. For example, there are the following json data.
Json_data4 = {"key1": {"key1_1": "value1_1"}, "key2": {"key2_1": ["a", "b", "c"]}}
In the face of slightly more complex json data, you can first define the string of the expression, and then use the search function for data lookup. For example, you need to find the following array in the json data.
# ["a", "b", "c"] exp = "key2.key2_1 [0]" res = jp.search (exp, json_data4) print ("values in the array:", res) # values in the array: a
So, how to use slicing in json data to find the json data you need is also supported.
Json_data5 = [0,1,2,3,4,5,6,7,8,9] exp = "[0:7]" res = jp.search (exp, json_data5) print ("slice data results", res) # slice data results [0,1,2,3,4,5,6]
Another search method is through the * sign wildcard, which is mainly used in json data with an array of outer data, such as the following data.
Json_data6 = {"data": [{"name": "Python concentration camp", "age": "5 years"}, {"name": "Sir.wang", "age": "28"}, {"dr": "nrg"}]}
In this form of data, the expression can be written like this. First find data as the key. At this time, you can use data [*] to find all the data under data and match it through the key at the next level.
Exp = "data [*] .name" res = jp.search (exp,json_data6) print (res) # ['Python concentration camp', 'Sir.wang']
If the key you want to match is dr, modify the exp expression to the following so that you can find it.
Exp = "data [*] .dr" # the result of the search is as follows. # ['nrg'] above is all the content of the article "how Python uses jmespath module to process json data". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.