user11338912
user11338912

Reputation:

i there a simple way to get these json values using python 3

EDIT: used the suggested solution, now the json has more headers, as shown bellow.

I have input file as Json,

{
    "systems-under-test": [{
            "type": "url",
            "sytems": [
                "www.google.com",
                "www.google.com",
                "www.google.com"
            ]
        },
        {
            "type": "api",
            "sytems": [
                "api.com",
                "api.fr"
            ]
        },
        {
            "type": "ip",
            "sytems": [
                "172.168 .1 .1",
                "172.168 .1 .0"
            ]
        }
    ],
    "headers-configuration": [{
        "custom": true,
        "headers-custom-configuration": {
            "headers": [{
                    "header-name": "x - frame - options",
                    "ignore": false,
                    "expected-value": ["deny", "sameorigin"]
                },
                {
                    "header-name": "content-security-policy",
                    "ignore": false,
                    "expected-value": []
                }
            ]
        }
    }],
    "header-results": []
}

after using the suggested solution by, I created dict that stores each header information and added these dict to a list. for a single header in Json works fine, when I have multi headers its not working.

     def load_header_settings2(self):
        header_custom_setting = []
        newDict = {}

        path = self.validate_path()
        with open(path) as json_file:
            data = load(json_file)
            config = data["headers-configuration"][0]
            if config["custom"]:
                headers = config["headers-custom-configuration"]["headers"]
                headers_name = headers["header-name"]
                ignore = headers["ignore"]
                expected_values = headers["expected-value"]

                newDict["header name"] = headers_name
                newDict["ignore"] = ignore
                newDict["expected value"] = expected_values
                header_custom_setting.append(newDict)
                newDict.clear()
                for i in header_custom_setting:
                    print(i)
                return header_custom_setting

can someone help?

Upvotes: 0

Views: 78

Answers (2)

user11338912
user11338912

Reputation:

This has been solved like this, is this safe way to reach my goal ?@ RoadRunner

    def load_header_settings2(self):
        header_custom_setting = []
        newDict = {}

        path = self.validate_path()
        with open(path) as json_file:
            data = load(json_file)
            config = data["headers-configuration"][0]
            if config["custom"]:
                headers = config["headers-custom-configuration"]["headers"]
                for header in headers:
                    headers_name = header["header-name"]
                    ignore = header["ignore"]
                    expected_values = header["expected-value"]

                    newDict["header name"] = headers_name
                    newDict["ignore"] = ignore
                    newDict["expected value"] = expected_values
                    header_custom_setting.append(newDict)
                    for i in header_custom_setting:
                        print(i)

                return header_custom_setting

Upvotes: 1

RoadRunner
RoadRunner

Reputation: 26325

You could get both header name and values like this, making sure we check "custom" is set to true before proceeding:

from json import load

with open("data.json") as json_file:
    data = load(json_file)
    config = data["headers-configuration"][0]

    if config["custom"]:
        headers = config["headers-custom-configuration"]["headers"]

        headers_name = headers["header-name"]
        print(headers_name)

        expected_values = headers["expected-value"]
        print(expected_values)

Output:

x - frame - options
['deny', 'sameorigin']

As for concatenating the headers and values, you could iterate the headers and values and combine them together into a string:

for value in expected_values:
    print("%s %s" % (headers_name, value))

Or using f-strings:

for value in expected_values:
    print(f"{headers_name} {value}")

Which will give you:

x - frame - options deny
x - frame - options sameorigin

Upvotes: 0

Related Questions