Hi,
Need help please. I want to change the all the key names under the key ‘source’ under all occurrences of the key ‘identifiers’ from name->fieldname and value to fieldvalue. However, I don’t want to change the keys where there are the keys: name, code, value.
I tried this but it is not correct:
with open(r’C:\Users\Administrator\Documents\test\PJSON.json’) as f:
payrolldata = json.load(f)
source = payrolldata[1][‘data’][‘workers’][1][‘codes’][1][‘source’]
print(source)
oldvalue = source.keys()
print(str(oldvalue).replace(‘name’, ‘newname’).replace(‘value’, ‘value2’))
payrolldata = str(oldvalue).replace(‘name’, ‘newname’).replace(‘value’, ‘newvalue2’)
for d in payrolldata:
d[‘newName’:] = d.pop[“‘name’:”]with open(r’C:\Users\Administrator\Documents\test\PJSON.json’, “w”) as f:
json.dump(payrolldata, f, indent=4)
{
"timestamp": "2022-09-20T08:16:00.000Z",
"metadata": {
"orgID": "6780",
"projectId": 0988,
}
},
{
"data":
"workers": [
{
"**identifiers**": {
"FullName": null,
"NINumber": null,
"CompID": null
},
"lastName": null,
"costCenter": null
},
{
"codes": [
{
"source": {
"**name**": "net_salary",
"**value**": 11500
},
"name": "net_salary",
"code": "rt_sa",
"value": 11500
},
{
"**identifiers**": {
"FullName": null,
"NINumber": null,
"Comp ID": null
},
"lastName": null,
"costCenter": null
},
{
"codes": [
{
"source": {
"**name**": "hiredate",
"**value**": 3.333
},
"name": "hiredate",
"code": "h_code",
"value": 3.333
},