Home > Software design >  check the boolean value of json Python
check the boolean value of json Python

Time:11-29

Hello I am pretty new working with AWS and SF I am trying to send information and I need to check if the list of json I am checking the information. I have the next json list:

here...b'[{
   "id": "xxxx",
   "success": true,
   "errors": []
   },
   {
   "id": "yyyy",
   "success": true,
   "errors": []
   }
  ]'

and in my lambda I do the next check:

response = requests.request("PATCH", url, headers=headers, data=body)
 print('here...' str(response.content))
if response.status_code == 200:
    for iResult in response.content.b["success"]:
        if iResult["success"] == false:
            raise Exception('Error. Check SF size fields...')

I want to make sure that every 'success' in every json is equals to True. And if it is false to raise an Exception. So I made a loop to iterate over each json but the problem I have is that I do not know how to access the json correctly. What is confusing me is the " b' " in the json I print. Could anybody help me? Thanks

CodePudding user response:

The b in the beginning means you have bytes, instead of a string, meaning you first have to convert your response content into a dictionary (think of a python term for a json) so that you can access the data in your response by their keys. Luckily that's easy to from a requests response with

json_data = response.json()
for sub_dict in json_data:
    if sub_dict ["success"] is False:
        raise ValueError("Check SF size fields...")

More about requests.response: https://requests.readthedocs.io/en/latest/api/#requests.Response

  • Related