Home > front end >  How can I assign values to a json file manually
How can I assign values to a json file manually

Time:09-18

I am new to Python and I have this json file which I want to build manually by assinging data to the values. An example below is the variable of teacher. If I print my json, I want the apiStatus value be replaced with the variable

        import pprint
        import json

        def testbobo():
                
                variable = "Teacher"

                jsonfile = f''' {
                                "apiStatusInfo": {
                                    "apiStatus": {variable},
                                    "apiStatusCode": 100,
                                    "apiDescription": "Error with folder 'Admin'",
                                    "apiErrorCode": 404,
                                    "apiErrorDescription": [
                                        {
                                            "reason": "invalid_parameter",
                                            "name": "item",
                                            "message": "Invalid value 'd_1097790817140'. not found"
                                        }
                                    ]
                                }
                            }'''
               
                print(jsonfile)

        testbobo()

When I tried this, I got the error message below. How can I build a json file manually

SyntaxError: f-string: expressions nested too deeply

CodePudding user response:

You can use double curly brackets, eg:

def testbobo():
    variable = "Teacher"
    jsonfile = f"""\
{{
                    "apiStatusInfo": {{
                        "apiStatus": {variable},
                        "apiStatusCode": 100,
                        "apiDescription": "Error with folder 'Admin'",
                        "apiErrorCode": 404,
                        "apiErrorDescription": [
                            {{
                                "reason": "invalid_parameter",
                                "name": "item",
                                "message": "Invalid value 'd_1097790817140'. not found"
                            }}
                        ]
                    }}
}}"""

    print(jsonfile)


testbobo()

Prints:

{
                    "apiStatusInfo": {
                        "apiStatus": Teacher,
                        "apiStatusCode": 100,
                        "apiDescription": "Error with folder 'Admin'",
                        "apiErrorCode": 404,
                        "apiErrorDescription": [
                            {
                                "reason": "invalid_parameter",
                                "name": "item",
                                "message": "Invalid value 'd_1097790817140'. not found"
                            }
                        ]
                    }
}

But I recommend to use json.dumps on standard Python dictionary:

import json


def testbobo():
    variable = "Teacher"

    data = {
        "apiStatusInfo": {
            "apiStatus": variable,
            "apiStatusCode": 100,
            "apiDescription": "Error with folder 'Admin'",
            "apiErrorCode": 404,
            "apiErrorDescription": [
                {
                    "reason": "invalid_parameter",
                    "name": "item",
                    "message": "Invalid value 'd_1097790817140'. not found",
                }
            ],
        }
    }

    print(json.dumps(data, indent=4))


testbobo()

Prints:

{
    "apiStatusInfo": {
        "apiStatus": "Teacher",
        "apiStatusCode": 100,
        "apiDescription": "Error with folder 'Admin'",
        "apiErrorCode": 404,
        "apiErrorDescription": [
            {
                "reason": "invalid_parameter",
                "name": "item",
                "message": "Invalid value 'd_1097790817140'. not found"
            }
        ]
    }
}
  • Related