1

I'm new using python and I would like to know how can I print some values from my json file using python, the following is my json file:

{
    "igt@gem_reloc_overflow@single-overflow": {
        "__type__": "TestResult",
        "command": "/home/gfx/intel-graphics/intel-gpu-tools/tests/gem_reloc_overflow --run-subtest single-overflow",
        "dmesg": "",
        "environment": "PIGLIT_PLATFORM=\"mixed_glx_egl\" PIGLIT_SOURCE_DIR=\"/home/gfx/intel-graphics/intel-gpu-tools/piglit\"",
        "err": "(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\nSubtest single-overflow failed.\n**** DEBUG ****\n(gem_reloc_overflow:19562) DEBUG: relocation_count=4294967295\n(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\n****  END  ****\n",
        "exception": null,
        "out": "IGT-Version: 1.14-g1e9a3ac (x86_64) (Linux: 4.6.0-rc4-drm-intel-nightly-ww17-commit-1e81bac+ x86_64)\nStack trace:\n  #0 [__igt_fail_assert+0x101]\n  #1 [reloc_tests+0x6d6]\n  #2 [<unknown>+0x6d6]\nSubtest single-overflow: FAIL (8.469s)\n",
        "pid": 19562,
        "result": "fail",
        "returncode": 99,
        "subtests": {
            "__type__": "Subtests"
        },
        "time": {
            "__type__": "TimeAttribute",
            "end": 1462072402.5360818,
            "start": 1462072393.7328644
        },
        "traceback": null
    }
}

The value that I need is "result : fail".

So far I have this code:

import json

with open("9.json") as json_file:
json_data = json.load(json_file)
print(json_data)
2

4 Answers 4

1

Give this a shot!

for key, value in json_data.iteritems():
    result = value['result']

print result

UPDATE (for question in comments): If you have multiple files and want to store all of the info at once - try throwing it all into a dictionary. This can vary depending on what you want the key to be. But try this (this will create a dicitonary of {json_key: result_value}:

all_results = {}
json_file_list = ['file_1.json', 'file_2.json']
for file in json_file_list:
    with open(file) as json_file:
        json_data = json.load(json_file)
        for key, value in json_data.iteritems():
            if 'result' in value:
                all_results[key] = value['result']
return all_results
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it works for me, i dont know if i can ask for one thing more, but if it is possible is there some way to improved this script, i mean i have 100 jsons in a folder, each one with different result, it is possible iterate on them and storage their differents results in several variables ? the results will be : fail, pass, dmesg-warn, dmesg-fail, skip, incomplete. if it is not possible i wll understand thanks
1

The json.load function returns a dictionary (an object of type dict).

A dictionary associates keys with vlues. To access values inside the dictionary, you can use this syntax:

value = dictionary[key]

In your particular case:

result = json_data['result']

Comments

0
json_data["igt@gem_reloc_overflow@single-overflow"]["result"]

this will print result

1 Comment

Why? Could you please add some context to your code.
0

The json data is stored as a python dictionary.

Hence you can access the value of "result" as follows:

json_data["igt@gem_reloc_overflow@single-overflow"]["result"]

W.R.T your code:

import json

with open("9.json") as json_file:
        json_data = json.load(json_file)


print(json_data["igt@gem_reloc_overflow@single-overflow"]["result"])

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.