Ascii codec cannot decode byte 0xc3 at position 3
Not sure why this error comes from a special character in the value, I am trying to get the key value from json using this recursive function provided by @Brien.
http://stackoverflow.com/questions/31010299/json-get-key-path-in-nested-dictionary
import json
with open('data.json') as data_file:
j = json.load(data_file)
def find(element, JSON,path,all_paths):
if element in JSON:
path = path + element + ' = ' + JSON[element].encode('utf-8')
print path
all_paths.append(path)
for key in JSON:
if isinstance(JSON[key], dict):
find(element, JSON[key],path + key + '.',all_paths)
all_paths = []
find('userNameField',j,'',all_paths)
print all_paths
0
source to share
2 answers
There are a couple of errors in the code:
- When calling an element from JSON, you must assign the variable, then encode or encode when you load it.
- When you encode a string that is a type
json.load
, you change it to byte. Addingstr
+byte
after the variable is correctly encoded will result in another error.
Try the following:
j = json.load(data_file, encoding='utf-8')
#....
path = path + element + ' = ' + str(JSON[element])
0
source to share