I use Esprima
to generate AST of a modern JSX component. It works great. I serilize that AST to a JSON in a file and load it in a Python file and I want to traverse each node in it.
I tried the esprima-ast-visitor but I get this error:
visitor.UnknownNodeTypeError: ImportDeclaration
I guess it's because that's an old library. I have also seen some examples on how to visit nodes in a JSON, yet they are all about specific examples on how to extract such value at such depth, or they are about searching.
But I want to be able to visit every node of a given JSON, and at the same time have access to its direct parent and children. How can I do that?
Basically I need a method with this signature:
import json
jsonString=open('json-path').read()
jsonRoot=json.loads(jsonString)
# this is a pseudo code, invalid in Python syntax, written in JS style
jsonRoot.visit(({ node, parent, children }) => {
# do something with the node here
})
P.S. I'm new to Python. That's why I can't write it.