fork download
  1. import json, copy
  2. from io import StringIO
  3.  
  4. sampleFile = StringIO('''
  5. [{"something": "else"}, {"eventWithUnknownName": "ArmReoriented", "visible": true}]
  6. ''')
  7.  
  8. def hideReorientedArm(obj):
  9. if isinstance(obj, dict):
  10. if obj.get("visible") is True:
  11. foundArmReoriented = False
  12. for (k,v) in obj.items():
  13. if v == "ArmReoriented":
  14. foundArmReoriented = True
  15. break
  16. if foundArmReoriented:
  17. obj["visible"] = False
  18. return obj
  19.  
  20. def walk(obj, updateFn):
  21. if isinstance(obj, list):
  22. obj = [walk(elem, updateFn) for elem in obj]
  23. elif isinstance(obj, dict):
  24. obj = {k: walk(v, updateFn) for k, v in obj.items()}
  25. return updateFn(obj)
  26.  
  27. data = json.load(sampleFile)
  28. data = walk(data, hideReorientedArm)
  29. print(json.dumps(data))
  30.  
Success #stdin #stdout 0.02s 9792KB
stdin
Standard input is empty
stdout
[{"something": "else"}, {"eventWithUnknownName": "ArmReoriented", "visible": false}]