-1

I want to format json string with **dict like normal string, but here somthing wrong with my code, how to do to meet my requirements?

Here's my code:

varDict = {"age": 18, "gender": "male"}
jsonStringVar = """
    {
        "persion":
            {
                "age": "{age}", 
                "gender": "{gender}"
            }
    }
"""

print(jsonStringVar.format(**varDict))

And Exception as below:

Exception has occurred: KeyError
'\n        "persion"'
  File "..\test_format.py", line 43, in <module>
    print(jsonStringVar.format(**varDict))
Silence He
  • 69
  • 10

1 Answers1

-1

Reference: How can I print literal curly-brace characters in python string and also use .format on it?

Solution:

jsonStringVar = """
    {{
        "persion":
            {{
                "age": "{age}", 
                "gender": "{gender}"
            }}
    }}
"""
Silence He
  • 69
  • 10