Ad

How To Parse Through A Nest Of Nested JSON?

- 1 answer

I am stuck using this API at my work, and having trouble parsing through the dataset because of it's complexity.

In the following JSON, the only values of any importance to me are "name" and the actual host name. I am trying to make a dictionary that consists of {"name":"host, host, host, host"}. If anyone knows how to parse through this or can point me in the right direction, that would be very much appreciated.

{

    "hugeData":[
    {

        "env1":[
        {

            "sins":[
            {"host": "ip-10-12-138-225.va1.b2c.test.com", "deployTime": "2015-07-23 11:54 AM", "sin": "0"},
            {"host": "ip-10-12-129-193.va1.b2c.test.com", "deployTime": "2015-09-01 01:09 PM", "sin": "7"},
            {"host": "ip-10-12-138-235.va1.b2c.test.com", "deployTime": "2015-07-23 11:54 AM", "sin": "9"},
            {"host": "ip-10-12-138-250.va1.b2c.test.com", "deployTime": "2015-07-23 11:53 AM", "sin": "12"},
            {"host": "ip-10-12-138-223.va1.b2c.test.com", "deployTime": "2015-07-23 11:53 AM", "sin": "14"},
            {"host": "ip-10-12-138-237.va1.b2c.test.com", "deployTime": "2015-07-23 11:54 AM", "sin": "17"},
            {"host": "ip-10-12-138-244.va1.b2c.test.com", "deployTime": "2015-07-23 11:53 AM", "sin": "18"},
            ],

            "status": "success",

            "buildTime": "2015-05-26T17:06:06",

        }
        ],

        "name": "apache-6"
        },

    {
        "env1":[
        {
            "sins":[
            {"host": "ip-10-12-138-225.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "0"},
            {"host": "ip-10-12-129-193.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "7"},
            {"host": "ip-10-12-138-235.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "9"},
            {"host": "ip-10-12-138-250.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "12"},
            {"host": "ip-10-12-138-223.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "14"},
            {"host": "ip-10-12-138-237.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "17"},
            {"host": "ip-10-12-138-244.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "18"},
            {"host": "ip-10-12-138-248.va1.b2c.test.com", "deployTime": "2015-12-16 05:23 PM", "sin": "21"},
            ],

            "status": "success",

            "buildTime": "2015-12-16T17:07:44",
        }
        ],

        "name": "apache-5"
        },

    {
        "env1":[
        {
            "sins":[
            {"host": "ip-10-12-138-234.va1.b2c.test.com", "deployTime": "2015-08-06 03:13 PM", "sin": "10"},
            {"host": "ip-10-12-138-246.va1.b2c.test.com", "deployTime": "2015-08-06 03:15 PM", "sin": "20"},
            {"host": "ip-10-12-138-216.va1.b2c.test.com", "deployTime": "2015-08-06 03:04 PM", "sin": "28"}
            ],

            "status": "success",

            "buildTime": "2013-02-21T15:41:59",

        }
        ],

        "name": "app-steel"
        },
}
Ad

Answer

Maybe you like the answer this time.

(assigned dict/json to a varibale named test)

your_dict = {i['name']: ", ".join([host['host'] for host in i['env1'][0]['sins']]) \
              for i in test["hugeData"]}
Ad
source: stackoverflow.com
Ad