Home > Software engineering >  how to add values to a nested dictionary json?
how to add values to a nested dictionary json?

Time:07-02

I'm trying to figure out how to add to a dictionary but not just any dictionary it a nested dictionary and relates to a recent question I asked here. How can to get the JSON out of webpage?. It turns out there are more logs that aren't on the site that I would need to manually add, right now I only know one, but would need to add this dictionary to the list it returns.

log_id = some_log_Id 
log_description = 'Digicert named log'
operator_name = 'Digicert'

So right now the code outputs this

[
    {
        "name": "Google",
        "logs": [
            {
                "description": "Google 'Argon2022' log",
                "log_id": "KXm 8J45OSHwVnOfY6V35b5XfZxgCvj5TV0mXCVdx4Q="
            },
            {
                "description": "Google 'Argon2023' log",
                "log_id": "6D7Q2j71BjUy51covIlryQPTy9ERa zraeF3fW0GvW4="
            },
            {
                "description": "Google 'Xenon2022' log",
                "log_id": "RqVV63X6kSAwtaKJafTzfREsQXS /Um4havy/HD bUc="
            },
            {
                "description": "Google 'Xenon2023' log",
                "log_id": "rfe  nz/EMiLnT2cHj4YarRnKV3PsQwkyoWGNOvcgoo="
            },
            {
                "description": "Google 'Icarus' log",
                "log_id": "KTxRllTIOWW6qlD8WAfUt2 /WHopctykwwz05UVH9Hg="
            },
            {
                "description": "Google 'Pilot' log",
                "log_id": "pLkJkLQYWBSHuxOizGdwCjw1mAT5G9 443fNDsgN3BA="
            },
            {
                "description": "Google 'Rocketeer' log",
                "log_id": "7ku9t3XOYLrhQmkfq GeZqMPfl wctiDAMR7iXqo/cs="
            },
            {
                "description": "Google 'Skydiver' log",
                "log_id": "u9nfvB KcbWTlCOXqpJ7RzhXlQqrUugakJZkNo4e0YU="
            }
        ]
    },
    {
        "name": "Cloudflare",
        "logs": [
            {
                "description": "Cloudflare 'Nimbus2022' Log",
                "log_id": "QcjKsd8iRkoQxqE6CUKHXk4xixsD6 tLx2jwkGKWBvY="
            },
            {
                "description": "Cloudflare 'Nimbus2023' Log",
                "log_id": "ejKMVNi3LbYg6jjgUh7phBZwMhOFTTvSK8E6V6NS61I="
            }
        ]
    },
    {
        "name": "DigiCert",
        "logs": [
            {
                "description": "DigiCert Log Server",
                "log_id": "VhQGmi/XwuzT9eG9RLI x0Z2ubyZEVzA75SYVdaJ0N0="
            },
            {
                "description": "DigiCert Log Server 2",
                "log_id": "h3W/51l8 IxDmV 9827/Vo1HVjb/SrVgwbTq/16ggw8="
            },
            {
                "description": "DigiCert Yeti2022 Log",
                "log_id": "IkVFB1lVJFaWP6Ev8fdthuAjJmOtwEt/XcaDXG7iDwI="
            },
            {
                "description": "DigiCert Yeti2023 Log",
                "log_id": "Nc8ZG7 xbFe/D61MbULLu7YnICZR6j/hKu oA8M71kw="
            },
            {
                "description": "DigiCert Nessie2022 Log",
                "log_id": "UaOw9f0BeZxWbbg3eI8MpHrMGyfL956IQpoN/tSLBeU="
            },
            {
                "description": "DigiCert Nessie2023 Log",
                "log_id": "s3N3B GEUPhjhtYFqdwRCUp5LbFnDAuH3PADDnk2pZo="
            },
            {
                "description": "DigiCert Yeti2022-2 Log",
                "log_id": "BZwB0yDgB4QTlYBJjRF8kDJmr69yULWvO0akPhGEDUo="
            }
        ]
    },
    {
        "name": "Sectigo",
        "logs": [
            {
                "description": "Sectigo 'Sabre' CT log",
                "log_id": "VYHUwhaQNgFK6gubVzxT8MDkOHhwJQgXL6OqHQcT0ww="
            },
            {
                "description": "Sectigo 'Mammoth' CT log",
                "log_id": "b1N2rDHwMRnYmQCkURX/dxUcEdkCwQApBo2yCJo32RM="
            }
        ]
    },
    {
        "name": "Let's Encrypt",
        "logs": [
            {
                "description": "Let's Encrypt 'Oak2022' log",
                "log_id": "36Veq2iCTx9sre64X04 WurNohKkal6OOxLAIERcKnM="
            },
            {
                "description": "Let's Encrypt 'Oak2023' log",
                "log_id": "tz77JN cTbp18jnFulj0bF38Qs96nzXEnh0JgSXttJk="
            }
        ]
    },
    {
        "name": "TrustAsia",
        "logs": [
            {
                "description": "Trust Asia Log2022",
                "log_id": "w2X5s2VPMoPHnamOk9dBj1ure MlLJjh0vBLuetCfSM="
            },
            {
                "description": "Trust Asia Log2023",
                "log_id": "6H6nZgvCbPYALvVyXT/g4zG5OTu5L79Y6zuQSdr1Q1o="
            }
        ]
    }
]

and I would just need to add the log_id and description piece underneath the Digicert piece but I have been trying to do it for a few hours and I'm just stuck. I've tried iterating and finding the Digicert name and appending the values but getting a need integer not str when I try pointing to the Digicert name value. Any help would be appreciated.

CodePudding user response:

You can iterate your list of dictionaries, searching for a match with operator_name on the name key, and if found, appending a dictionary of log_description and log_id to the logs list. If not found, you would append a new dictionary (with name and logs properties) to the list:

log_id = 'some_log_Id'
log_description = 'Digicert named log'
operator_name = 'Digicert'

for d in obj:
    if d['name'] == operator_name:
        d['logs'].append({ 'description': log_description, 'log_id': log_id })
        break
else:
    obj.append({ 'name' : operator_name, 'logs': [ { 'description': log_description, 'log_id': log_id } ] })
  • Related