简体   繁体   中英

Using regex to extract data from JSON response in Python

I'm trying to extract a string from a JSON response using regex in Python, but with no success.

{"ao":["jskl|_xx2|020|b503414ff19853ce357413fafe7c612a0b6b0ba3f592f9b551bdc8d0dbdbbd34:J26U1IfsvZ0kiJwLm3xoVhZNN/Xr+Z2gRkJA===|true|900"]}

I'm trying to get

b503414ff19853ce357413fafe7c612a0b6b0ba3f592f9b551bdc8d0dbdbbd34:J26U1IfsvZ0kiJwLm3xoVhZNN/Xr+Z2gRkJA=== 

from the string. However, the | in the string won't allow me to use the methods I have seen on Stack Overflow because it keeps missing the | . I would appreciate any help.

here it is, you can escape special characters inside character class:

import re
text = '{"ao":["jskl|_xx2|020|b503414ff19853ce357413fafe7c612a0b6b0ba3f592f9b551bdc8d0dbdbbd34:J26U1IfsvZ0kiJwLm3xoVhZNN/Xr+Z2gRkJA===|true|900"]}'


match = re.search(r'[|]b.*===[|]', text).group()[1:-1]
print(match)

output:

b503414ff19853ce357413fafe7c612a0b6b0ba3f592f9b551bdc8d0dbdbbd34:J26U1IfsvZ0kiJwLm3xoVhZNN/Xr+Z2gRkJA===

There's no need to reinvent json.loads() with regex. Parse your JSON string to a dictionary with json.loads() and access the string you're interested in by indexing into the dictionary. Once you've extracted the string, split on the pipe character and access the third index of the list:

your_json_dict_name["ao"][0].split("|")[3]

Here's a full example:

import json

raw_json_str = r'{"ao":["jskl|_xx2|020|b503414ff19853ce357413fafe7c612a0b6b0ba3f592f9b551bdc8d0dbdbbd34:J26U1IfsvZ0kiJwLm3xoVhZNN/Xr+Z2gRkJA===|true|900"]}'
json_dict = json.loads(raw_json_str)

print(json_dict["ao"][0].split("|")[3])

Output:

b503414ff19853ce357413fafe7c612a0b6b0ba3f592f9b551bdc8d0dbdbbd34:J26U1IfsvZ0kiJwLm3xoVhZNN/Xr+Z2gRkJA===

Ok so, for starters i dont quite understand why are you not using json.loads on this string, so you could refer to this json as a map and go to "ao" key, and use the regex on the strings inside the array.

But putting that aside, if you still with to extract the data from the json as string, you could use regex groups and some escaping ("\\") on the "|" character.

Which would look somwthing like this :

. ?[\\"(. ?\\|){3}(. ?)\\|.

Then you can access group 2 and get your desired result That assumming the json looks the same always

If your array on "ao" property has more than 1 string, this wont get the second value. Therefore i wouls suggest to transform this string into map before hand, and then loop every string on its own.

Good luck

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