0

I want something exactly similar to estraverse. I want to run a function for each node of this JSON object. I asked another question here but that did not solve my problem.

I want to be able to get each node, as is, and check it as a JSON object.

As an example, I want to see if a node's type is JSXText and it has a non-empty value, then print that node's value:

if node.type == 'JSXText' and node.value.strip() != '':
    print (node.value)

How can I get to that point? I can do this very easily in C#. Yet I can't transfer my knowledge to Python in this case.

Big boy
  • 1,113
  • 2
  • 8
  • 23
  • Adding your c# code would at least enlighten *some* readers . Currently there is too little to work with, but the target code will look similar to: `for node in json: function(node) ` – guidot Jun 21 '23 at 12:38
  • Does this answer your question? [Find all occurrences of a key in nested dictionaries and lists](https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists) – JonSG Jun 21 '23 at 14:11

2 Answers2

1

Looks like I have something working using recursion:

import json

def load_json_file(file_path):
    with open(file_path, "r") as f:
        data = json.load(f)
    return data

def iterate_json(json_obj):
    if isinstance(json_obj, dict) and "type" in json_obj:
        if json_obj["type"] == 'JSXText' and json_obj["value"].strip(' ') != '':
            print(json_obj[ "value" ].strip(' '))

    if isinstance(json_obj, dict):
        for key, value in json_obj.items():
            if isinstance(value, (dict, list)):
                iterate_json(value)
            
    elif isinstance(json_obj, list):
        for item in json_obj:
            if isinstance(item, (dict, list)):
                iterate_json(item) 
            

jj = load_json_file("your_file.json")

iterate_json(jj)

Hope that's what you need. Careful that .strip() method will remove all the '\n' and ' ' at the beginning and the end of your string. And you said in the question that you wanted a non empty-value so I adjusted the condition to json_obj["value"].strip(' ') != ''.

matleg
  • 618
  • 4
  • 11
1

try to understand your question about json
you want to travel all nodes about a json file
and find the key=='JSTEXT' or print it to screen, I write a codes

import json

def readjson():
    inpath = r'C:\Users\10696\Desktop\access\6\flowover_kkkk.json'
    with open(inpath, 'r') as obj:
        fil = json.load(obj)
    return fil

result = []
def travel(jsonfile):
    global result
    for key, value in jsonfile.items():
        if isinstance(value, list):
            for i in value:
                travel(i)
        elif isinstance(value, dict):
            travel(value)
        else:
            result.append([key, value])
    
if __name__=="__main__":
    fil = readjson()
    travel(fil)
    for key, value in result:
        # if key=='JSXText' and len(value)!=0:
        print(key, value)

Considering this json file have no result which always "len(value)==0", so I mark this line

        # if key=='JSXText' and len(value)!=0:

this is the json file I used in codes JSON object, you can save it with the name "flowover_kkkk.json" using vscode IDE.
one more thing, you should revise this line to keep it in json format.

                          "computed": false,JSXText

to

                          "computed": "false,JSXText",
Jiu_Zou
  • 463
  • 1
  • 4