Tim Bond
Tim Bond

Reputation: 11

How can I add if statements to a json dictionary in Python

Is it possible to change a json dictionary payload with if statements? I have 4 types that say have IDs 1,2,3,4 so I need some sort of select switch code.

The original code is:

payload = {
    "added": [
        {
            "assingedTargets": [
                {
                    "sequencing": 1,
                        "name": assigned_target_name,
                        "id": assigned_target_id
                    }
                ],
                "type": {
                    "protocol": {
                        "isDefaultProtocol": True,
                        "name": "tcp",
                        "id": 3,
                        "label": "tcp"
                    },
                    "name": target_type,
                    "id": int(1)
                },
              "name":"DDITargetGroup5"        
            }
        ]
        }

What I would like to do is:

payload = {
    "added": [
        {
            "assingedTargets": [
                {
                    "sequencing": 1,
                        "name": assigned_target_name,
                         "id": assigned_target_id
                    }
                ],
                "type": {
                    "protocol": {
                        "isDefaultProtocol": True,
                        "name": "tcp",
                        "id": 3,
                        "label": "tcp"
                    },
                    if target_type: == "direct dial" 
                      "name": target_type,
                      "id": int(1)
                    elif target_type == "private Wire"
                      "name": target_type
                      "id": int(2) 
                },
              "name":"DDITargetGroup5"        
            }
        ]
        }

I get the error:

if target_type: == "direct dial"
             ^
SyntaxError: invalid syntax

Process finished with exit code 1

Upvotes: 1

Views: 6808

Answers (2)

Railslide
Railslide

Reputation: 5554

If there are just two options, you could use the ternary operator

{
     ...
     "name": target_type,
     "id": 1 if target_type == 'direct dial' else 2,
     ... 
}

However, it comes with a cost of readability, so you should consider whether wouldn't be better to separate the conditional statement from the dict.

Upvotes: 1

pzp
pzp

Reputation: 6597

You can't just plop a conditional statement into a json object. Instead you should create a variable to store the id value (id_val) and use a conditional before you make the object to set its value based on the value of target_type. Then set the dictionary to have the values of those variables (see below).

id_val = 0
if target_type == 'direct dial':
    id_val = 1
elif target_type == 'private Wire':
    id_val = 2

payload = { "added": [ { "assingedTargets": [ { "sequencing": 1, "name": assigned_target_name, "id": assigned_target_id } ], "type": { "protocol": { "isDefaultProtocol": True, "name": "tcp", "id": 3, "label": "tcp" }, "name": target_type, "id": id_val }, "name":"DDITargetGroup5" } ] }

Upvotes: 0

Related Questions