Home > Software engineering >  Reading a section in a json result and adding to a python list
Reading a section in a json result and adding to a python list

Time:09-15

I received the json below as response from an api call

                {
                    "apiStatusInfo": {
                        "apiStatus": "Success",
                        "apiStatusCode": 0
                    },
                    "errors": [
                        {
                            "userEmail": "man.done.banner.com",
                            "error": "Invalid input parameters in request"
                        },
                        {
                           "userEmail": "ban.super.banner.com",
                           "error": "Invalid input parameters in request"
                        }
                    ]
               } 
   

I want to be able to read the errors section and append it to a list so I can send it out via email. I created this function

        def testbobo():

                my_data = f'''{
                                "apiStatusInfo": {
                                    "apiStatus": "Success",
                                    "apiStatusCode": 0
                                },
                                "errors": [
                                    {
                                        "userEmail": "man.done.banner.com",
                                        "error": "Invalid input parameters in request"
                                    },
                                    {
                                        "userEmail": "ban.super.banner.com",
                                        "error": "Invalid input parameters in request"
                                    }
                                ]
                           }'''
               
                
                converter = json.load(my_data)
                for i in converter['errors']:
                    print(i)

        testbobo()

All I have been getting is "ValueError: Invalid format specifier"

what is wrong with this? How can I extract the errors section and add to a python list?

CodePudding user response:

Two problems:

  1. You are defining an F-string, but the accolades {} have special significance, so they should be escaped (doubled) like this: {{. Or simply removing the leading f should be enough to fix the problem.
  2. And then you need to write: json.loads(my_data) instead of: json.load(my_data) because you are loading a dict from a string, not from a file.

But I don't think you are doing any string interpolation here, so you don't need a string in the first place, just a regular dict. In other words, you should be able to cast the response as dict right away.

CodePudding user response:

Try:

import json

data = """\
{
    "apiStatusInfo": {
        "apiStatus": "Success",
        "apiStatusCode": 0
    },
    "errors": [
        {
            "userEmail": "man.done.banner.com",
            "error": "Invalid input parameters in request"
        },
        {
           "userEmail": "ban.super.banner.com",
           "error": "Invalid input parameters in request"
        }
    ]
}"""

data = json.loads(data)

errors = []
emails = []
for e in data.get("errors", []):
    err = e.get("error", "N/A")
    errors.append(err)

    em = e.get("userEmail", "N/A")
    emails.append(em)

for err, em in zip(errors, emails):
    print(em, err)

Prints:

man.done.banner.com Invalid input parameters in request
ban.super.banner.com Invalid input parameters in request
  • Related