I am trying to change a specific nested value ("pH") if the "ID" matches , but it only changes the first one and not the one I intended.
What I am trying to do:
{
"type": "FeatureCollection",
"name": "test",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::3059"
}
},
"features": [{
"type": "Feature",
"properties": {
"ID": 1,
"pH": 3.5,
"P": 2.8,
"K": 11.0,
"Mg": 15.8
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
]
]
}
}, {
"type": "Feature",
"properties": {
"ID": 2,
"pH": 3,
"P": 2.5,
"K": 11.1,
"Mg": 15.8
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
]
]
}
}
]
}
But it changes the "pH value where "ID" is 1 and "pH" value where "ID": 2 stays the same.
Here is my code:
import json
with open('filepath', 'r+') as f:
data = json.load(f)
for feature in data['features']:
print(feature['properties'])
if feature['properties']["ID"] == 2:
data['features'][0]['properties']["pH"]=10
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()