Python JSON Manipulation: Finding and Updating Object by ID
import json
json_data = '''
[
{
"id": "eId-75657625",
"source": "1",
"target": "75657625",
"label": "",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"id": "eId-506004445",
"source": "75657625",
"target": "506004445",
"label": "",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"id": "eId-965166501",
"source": "506004445",
"target": "965166501",
"sourceHandle": "a",
"label": "True",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"id": "eId-290190680",
"source": "506004445",
"target": "290190680",
"sourceHandle": "b",
"label": "False",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"id": "eId-472887162",
"source": "290190680",
"target": "472887162",
"label": "",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"id": "eId-566016147",
"source": "472887162",
"target": "566016147",
"sourceHandle": "b",
"label": "False",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"id": "eId-834736475",
"source": "472887162",
"target": "834736475",
"sourceHandle": "a",
"label": "True",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20}
},
{
"source": "834736475",
"sourceHandle": null,
"target": "2",
"targetHandle": null,
"type": "buttonedge",
"id": "reactflow__edge-834736475-2"
},
{
"source": "566016147",
"sourceHandle": null,
"target": "2",
"targetHandle": null,
"type": "buttonedge",
"id": "reactflow__edge-566016147-2"
},
{
"id": "eId-866813082",
"source": "965166501",
"target": 2,
"label": "",
"style": {"strokeWidth": 2},
"markerEnd": {"type": "arrowclosed", "width": 20, "height": 20},
"type": "buttonedge"
}
]
'''
data = json.loads(json_data)
for obj in data:
if obj.get('id') == 'eId-472887162':
obj['target'] = '5555'
break
print(json.dumps(data, indent=4))
Output:
[
{
"id": "eId-75657625",
"source": "1",
"target": "75657625",
"label": "",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"id": "eId-506004445",
"source": "75657625",
"target": "506004445",
"label": "",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"id": "eId-965166501",
"source": "506004445",
"target": "965166501",
"sourceHandle": "a",
"label": "True",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"id": "eId-290190680",
"source": "506004445",
"target": "290190680",
"sourceHandle": "b",
"label": "False",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"id": "eId-472887162",
"source": "290190680",
"target": "5555",
"label": "",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"id": "eId-566016147",
"source": "472887162",
"target": "566016147",
"sourceHandle": "b",
"label": "False",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"id": "eId-834736475",
"source": "472887162",
"target": "834736475",
"sourceHandle": "a",
"label": "True",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
}
},
{
"source": "834736475",
"sourceHandle": null,
"target": "2",
"targetHandle": null,
"type": "buttonedge",
"id": "reactflow__edge-834736475-2"
},
{
"source": "566016147",
"sourceHandle": null,
"target": "2",
"targetHandle": null,
"type": "buttonedge",
"id": "reactflow__edge-566016147-2"
},
{
"id": "eId-866813082",
"source": "965166501",
"target": 2,
"label": "",
"style": {
"strokeWidth": 2
},
"markerEnd": {
"type": "arrowclosed",
"width": 20,
"height": 20
},
"type": "buttonedge"
}
]
原文地址: https://www.cveoy.top/t/topic/orTv 著作权归作者所有。请勿转载和采集!