2

I'm trying to output JSON that has a list of mac addresses and a list of timestamps associated with each mac address. The output I'm looking for would look like this:

[
"B2:C7:23:10:A0":
[
    "2014-04-04T21:30:46.348900800Z", 
    "2014-04-04T21:30:46.348900800Z", 
    "2014-04-04T21:30:46.348900800Z", 
    "2014-04-04T21:14:34.305303100Z", 
    "2014-04-04T21:14:34.285302000Z", 
    "2014-04-04T21:14:33.905280300Z"
], 
"C7:99:12:F2:00":
[
    "2014-04-09T22:18:43.162844700Z", 
    "2014-04-09T22:02:39.138705700Z", 
    "2014-04-09T22:02:37.429608000Z",
    "2014-04-09T22:02:36.966581500Z", 
    "2014-04-09T22:02:36.966581500Z", 
    "2014-04-09T22:02:36.966581500Z", 
],
]

Right now, the code I have makes the json above but with no keys (no mac addresses), only groups of timestamps.

    list_count = 0
    indices = []
    mac_times_array = []
    for foundMacAddress in found_mac_list:
        indices = [i for i, x in enumerate(macAddressesTotal) if x == foundMacAddress]
        grouped_times = []
        for index in indices:
            grouped_times.append(times[index])
        mac_times_array.append(grouped_times)


    stacked_array = [i for i in mac_times_array]
    pprint.pprint(json.dumps(stacked_array))

So my question is, how do I add the mac addresses as keys? I've tried a bunch of different things but nothing is working.

2 Answers 2

1

EDIT: See other comments below, OP's JSON wasn't valid as-is, but I think the meat of the question dealt with the "dictionary vs. array" issue, which I answered.

It looks like you're taking arrays and adding them to mac_times_array. You should instead be using a dictionary, and making foundMacAddress the key. Do something like this:

list_count = 0
indices = []
mac_times_dict = {}
for foundMacAddress in found_mac_list:
    indices = [i for i, x in enumerate(macAddressesTotal) if x == foundMacAddress]
    grouped_times = []
    for index in indices:
        grouped_times.append(times[index])
    mac_times_dict[foundMacAddress] = grouped_times


# stacked_array = [i for i in mac_times_array]
pprint.pprint(json.dumps(mac_times_dict))

(Warning, code not tested because I don't have your input data)

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe point out that the JSON won't look exactly like what was posted (the outer square braces will be replaced with curly braces). Very small difference, especially since the posted example isn't valid JSON, but this would avoid any surprises.
Thanks to both of you. I do indeed want valid JSON so I've changed my code to append the dictionary to an array like in shaktimaan's answer.
0

The output you are expecting is not a valid JSON. This is:

[
    {
        "B2:C7:23:10:A0": [
            "2014-04-04T21:30:46.348900800Z",
            "2014-04-04T21:30:46.348900800Z",
            "2014-04-04T21:30:46.348900800Z",
            "2014-04-04T21:14:34.305303100Z",
            "2014-04-04T21:14:34.285302000Z",
            "2014-04-04T21:14:33.905280300Z"
        ]
    },
    {
        "C7:99:12:F2:00": [
            "2014-04-09T22:18:43.162844700Z",
            "2014-04-09T22:02:39.138705700Z",
            "2014-04-09T22:02:37.429608000Z",
            "2014-04-09T22:02:36.966581500Z",
            "2014-04-09T22:02:36.966581500Z",
            "2014-04-09T22:02:36.966581500Z"
        ]
    }
]

To get this, you can do:

list_count = 0
indices = []
mac_times_array = []
for foundMacAddress in found_mac_list:
    mac_dict = {}
    indices = [i for i, x in enumerate(macAddressesTotal) if x == foundMacAddress]
    grouped_times = []
    for index in indices:
        grouped_times.append(times[index])
    mac_dict[foundMacAddress] = grouped_times
    mac_times_array.append(mac_dict)

pprint.pprint(json.dumps(mac_times_array))

I can not confirm this since I am not sure of the input list found_mac_list and macAddressesTotal

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.