0

I have this json file:

"compileFlags": {
    "useVarcharZplitterRemoveIf": false,
    "useVarcharZplitterSortVal": false,
    "useVarcharZplitterSortKey": false,
    "useVarcharZplitterJoinVal": false,
    "useVarcharZplitterJoinKey": true,
},
"runtimeFlags": {
    "useShortcutJoin": true,
    "useMemorySpool": true,
},
"runtimeGlobalFlags": {
    "useMetadataServer": true,
    "metadataServerIp1": 127,

},
"server":{
    "gpu": 0,
    "port": 5000,
}
}

I want to be able to count each object inside this json file , for example: compileFlags=5 (beacuse it got 18 items under its name) runtimeFlags=2

This is my code:

    with open(json_file_path, "r") as f:
        data = json.load(f)

    print(len(data["compileFlags"]))
    print(len[data["runtimeFlags"]])
    print(len[data["runtimeGlobalFlags"]])
    print(len[data["server"]])
read_json_file(path_to_json_file_location)

When I am running it I am getting the following error message:

TypeError: 'builtin_function_or_method' object is not subscriptable

What am I doing wrong?

4
  • 3
    There are typo in your code, len[data["runtimeFlags"]] should be len(data["runtimeFlags"]) and all lines below. Commented Jun 8, 2020 at 17:22
  • Oh yeah I missed that thanks! I Commented Jun 8, 2020 at 17:27
  • 1
    Below is the answer for your question. Commented Jun 8, 2020 at 17:28
  • @tupacshakur see my answer, this one you can use to count the elements in any json with your structure. Commented Jun 8, 2020 at 17:28

1 Answer 1

1

You can use the below function to return you a dictionary with the count of each element

count = {}
for k, v in data.items():
    count[k] = len(v)

print(count)

OUT: {'compileFlags': 5, 'runtimeFlags': 2, 'runtimeGlobalFlags': 2, 'server': 2}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.