-3

I want to create a JSON like that

{
    "beacons": {
        "0c:f3:ee:16:6f:15": {
            "label": "label",
            "major": "7",
            "minor": 15349,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        },
        "0c:f3:ee:16:6f:24": {
            "label": "label",
            "major": "7",
            "minor": 15364,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        },
        "0c:f3:ee:16:6f:53": {
            "label": "label",
            "major": "7",
            "minor": 15411,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        }
    }
}

I have already created beacon object like that

beacon = {
    advertiser_mac: {
        'major': major,
        'minor': minor,
        'uuid': proximity_uuid
    }
}

But I do not know how to concat all of them in one object like the example above

Amine Harbaoui
  • 1,247
  • 2
  • 17
  • 34
  • [You can insert new keys into dictionaries](https://docs.python.org/3/library/stdtypes.html#dict) – ForceBru Feb 15 '19 at 13:45
  • Possible duplicate of [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – gold_cy Feb 15 '19 at 13:46

1 Answers1

1

beacon is a dictionary. So, if you know how to generate the dictionary for each advertiser_mac, you can just do key assignment:

beacon = {}

advertiser_mac = "0c:f3:ee:16:6f:53"
advertiser_mac_dict = {
            "label": "label",
            "major": "7",
            "minor": 15411,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        }

beacon[advertiser_mac] = advertiser_mac_dict

beacon
{"0c:f3:ee:16:6f:53": {
            "label": "label",
            "major": "7",
            "minor": 15411,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        }
}
C.Nivs
  • 12,353
  • 2
  • 19
  • 44