I want change one json value with a Python 3.8 script.
I know in Python, strings are immutable, so you can't change their characters in-place.
This helps my much: How to find and replace a part of a value in json file
TypeError: 'str' object does not support item assignment
item['hotKey'] = "" TypeError: 'str' object does not support item assignment
item['hotKey'] = "<f11>"
from pathlib import Path
import json
home = str(Path.home())
path = home + "/.config/autokey/data/Sample Scripts/"
jsonFilePath = home + "/.config/autokey/data/Sample Scripts/.run-run-lintalistAHK-all.json"
with open(jsonFilePath) as f:
data = json.load(f)
for item in data['hotkey']:
item['hotKey'] = "<f11>" # item['hotKey'].replace('$home', item['id'])
with open(jsonFilePath, 'w') as f:
json.dump(data, f)
json file
{
"hotkey": {
"hotKey": "<f12>"
},
}
You need to add json['hotkey']
reference before:
for item in data['hotkey']:
data['hotkey'][item] = "<f11>" # item['hotKey'].replace('$home', item['id'])
My raw attempt:
import json
j = '''
{
"hotkey": {
"hotKey": "<f12>"
}
}
'''
data = json.loads(j)
for x in data['hotkey']:
data['hotkey'][x] = '<f11>'
print(data)