1

I am getting the json decode error when I try to hit the following json file for a post request. Please help

API Code:

import requests
from  utilities.configurations import *
from utilities.resources import *
#from PayLoads.plans.payload1A1C import *
from PayLoads.plans.testpayload import *


config = configparser.ConfigParser()
config.read('utilities/properties.ini')
url = getCOnfigurations()['API']['URL']+APIResources.endpoints_plans
response = requests.post(url, json= plan1A1C(),)

from behave.formatter import null
import json

def plan1A1C():
    body = {
        "payload": {
            "data": {
                "organization_code": "TEST",
                "quote_id": 71728,
                "quote_token": "odQ53i3SxCANUR68Lry3",
                "session_token": "odQ53i3SxCANUR68Lry3",
                "partner_code": "TEST",
                "category_code": "HEALTH_RETAIL",
                "insured": [
                    {
                        "person_id": 45820,
                        "name": null,
                        "occupation": null,
                        "annual_income": null,
                        "phone_number": "8789538796",
                        "email": null,
                        "age": 40,
                        "dob": "07-09-1981",
                        "gender": null,
                        "nominee_name": " ",
                        "nominee_dob": null,
                        "nominee_age": null,
                        "nominee_gender": null,
                        "appointee_name": " ",
                        "appointee_dob": null,
                        "relationship_with_appointee": null,
                        "appointee_gender": null,
                        "relationship_with_nominee": null,
                        "relation_with_primary_member": "self",
                        "height": null,
                        "weight": null,
                        "salutation": null,
                        "marital_status": null
                    },
                    {
                        "person_id": 45821,
                        "name": null,
                        "occupation": null,
                        "annual_income": null,
                        "phone_number": "8789538796",
                        "email": null,
                        "age": 10,
                        "dob": "07-09-2011",
                        "gender": null,
                        "nominee_name": " ",
                        "nominee_dob": null,
                        "nominee_age": null,
                        "nominee_gender": null,
                        "appointee_name": " ",
                        "appointee_dob": null,
                        "relationship_with_appointee": null,
                        "appointee_gender": null,
                        "relationship_with_nominee": null,
                        "relation_with_primary_member": "kid-1",
                        "height": null,
                        "weight": null,
                        "salutation": null,
                        "marital_status": null
                    }

                ],
                "primary_member_age": 40,
                "sum_insured": 100000,
                "pin_code": "751008",
                "payment_frequency": "SINGLE",
                "payment_option": null,
                "limited_pay": null,
                "policy_paying_term": null,
                "tobacco_habit": null,
                "risk_profile": null,
                "maturity_benefit_type": null,
                "investment_goal": null,
                "investment_amount": null,
                "cover_list": null,
                "insurance_cover_list": null,
                "posp": {
                    "name": "abcd",
                    "user_id": "9991112223",
                    "pan": null,
                    "aadhar": null,
                    "level": {
                        "id": 41,
                        "partner_id": 52,
                        "role": "ANAND_RATHI_staff",
                        "code": "ANAND_RATHI_POSP",
                        "superior_role_id": null,
                        "path": "//41",
                        "created_at": "2021-06-11T17:48:01.000+05:30",
                        "updated_at": "2021-06-12T16:42:40.000+05:30"
                    },
                    "staf_type": "POSP"
                },
                "product_code": "TEST",
                "insurance_code": "TEST",
                "tenure": 1
            },
            "config": {
                "coi_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicySchedule",
                "password": "in5uR@nceAnan0",
                "plan_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PremiumComputation",
                "username": "ptrn_anandinsurance",
                "token_url": "https://developer.TEST.com/endpoint/token",
                "grant_type": "password",
                "policy_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicyGeneration",
                "proposal_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/ProposalSave",
                "PaymentOption": "A",
                "header_password": "TEST",
                "header_username": "TEST",
                "IntermediaryCode": "2002954741760001",
                "UniqueTransactionID": "8765456711111111112232"
            }
        },
        "insurance_company_code": "TEST",
        "ic": "TEST",
        "insurance_code": "TEST",
        "product_code": "TEST",
        "category_code": "TEST",
        "callbackUrl": "https://api.iifl.TEST.com/api_gateway_callbacks/plans"
        }
    s1 = json.loads(str(body))
    return s1

For the above code, I am getting the follpowing error as "json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)". I have also checked that the property values in the json are inb double quotes only

2 Answers2

0

Try the below (note that body str is defined as a string)

import json

def foo():
    body_str = '''{
        "payload": {
            "data": {
                "organization_code": "TEST",
                "quote_id": 71728,
                "quote_token": "odQ53i3SxCANUR68Lry3",
                "session_token": "odQ53i3SxCANUR68Lry3",
                "partner_code": "TEST",
                "category_code": "HEALTH_RETAIL",
                "insured": [
                    {
                        "person_id": 45820,
                        "name": null,
                        "occupation": null,
                        "annual_income": null,
                        "phone_number": "8789538796",
                        "email": null,
                        "age": 40,
                        "dob": "07-09-1981",
                        "gender": null,
                        "nominee_name": " ",
                        "nominee_dob": null,
                        "nominee_age": null,
                        "nominee_gender": null,
                        "appointee_name": " ",
                        "appointee_dob": null,
                        "relationship_with_appointee": null,
                        "appointee_gender": null,
                        "relationship_with_nominee": null,
                        "relation_with_primary_member": "self",
                        "height": null,
                        "weight": null,
                        "salutation": null,
                        "marital_status": null
                    },
                    {
                        "person_id": 45821,
                        "name": null,
                        "occupation": null,
                        "annual_income": null,
                        "phone_number": "8789538796",
                        "email": null,
                        "age": 10,
                        "dob": "07-09-2011",
                        "gender": null,
                        "nominee_name": " ",
                        "nominee_dob": null,
                        "nominee_age": null,
                        "nominee_gender": null,
                        "appointee_name": " ",
                        "appointee_dob": null,
                        "relationship_with_appointee": null,
                        "appointee_gender": null,
                        "relationship_with_nominee": null,
                        "relation_with_primary_member": "kid-1",
                        "height": null,
                        "weight": null,
                        "salutation": null,
                        "marital_status": null
                    }

                ],
                "primary_member_age": 40,
                "sum_insured": 100000,
                "pin_code": "751008",
                "payment_frequency": "SINGLE",
                "payment_option": null,
                "limited_pay": null,
                "policy_paying_term": null,
                "tobacco_habit": null,
                "risk_profile": null,
                "maturity_benefit_type": null,
                "investment_goal": null,
                "investment_amount": null,
                "cover_list": null,
                "insurance_cover_list": null,
                "posp": {
                    "name": "abcd",
                    "user_id": "9991112223",
                    "pan": null,
                    "aadhar": null,
                    "level": {
                        "id": 41,
                        "partner_id": 52,
                        "role": "ANAND_RATHI_staff",
                        "code": "ANAND_RATHI_POSP",
                        "superior_role_id": null,
                        "path": "//41",
                        "created_at": "2021-06-11T17:48:01.000+05:30",
                        "updated_at": "2021-06-12T16:42:40.000+05:30"
                    },
                    "staf_type": "POSP"
                },
                "product_code": "TEST",
                "insurance_code": "TEST",
                "tenure": 1
            },
            "config": {
                "coi_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicySchedule",
                "password": "in5uR@nceAnan0",
                "plan_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PremiumComputation",
                "username": "ptrn_anandinsurance",
                "token_url": "https://developer.TEST.com/endpoint/token",
                "grant_type": "password",
                "policy_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicyGeneration",
                "proposal_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/ProposalSave",
                "PaymentOption": "A",
                "header_password": "TEST",
                "header_username": "TEST",
                "IntermediaryCode": "2002954741760001",
                "UniqueTransactionID": "8765456711111111112232"
            }
        },
        "insurance_company_code": "TEST",
        "ic": "TEST",
        "insurance_code": "TEST",
        "product_code": "TEST",
        "category_code": "TEST",
        "callbackUrl": "https://api.iifl.TEST.com/api_gateway_callbacks/plans"
        }'''
    s1 = json.loads(body_str)
    return s1

print(foo())

output

{'payload': {'data': {'organization_code': 'TEST', 'quote_id': 71728, 'quote_token': 'odQ53i3SxCANUR68Lry3', 'session_token': 'odQ53i3SxCANUR68Lry3', 'partner_code': 'TEST', 'category_code': 'HEALTH_RETAIL', 'insured': [{'person_id': 45820, 'name': None, 'occupation': None, 'annual_income': None, 'phone_number': '8789538796', 'email': None, 'age': 40, 'dob': '07-09-1981', 'gender': None, 'nominee_name': ' ', 'nominee_dob': None, 'nominee_age': None, 'nominee_gender': None, 'appointee_name': ' ', 'appointee_dob': None, 'relationship_with_appointee': None, 'appointee_gender': None, 'relationship_with_nominee': None, 'relation_with_primary_member': 'self', 'height': None, 'weight': None, 'salutation': None, 'marital_status': None}, {'person_id': 45821, 'name': None, 'occupation': None, 'annual_income': None, 'phone_number': '8789538796', 'email': None, 'age': 10, 'dob': '07-09-2011', 'gender': None, 'nominee_name': ' ', 'nominee_dob': None, 'nominee_age': None, 'nominee_gender': None, 'appointee_name': ' ', 'appointee_dob': None, 'relationship_with_appointee': None, 'appointee_gender': None, 'relationship_with_nominee': None, 'relation_with_primary_member': 'kid-1', 'height': None, 'weight': None, 'salutation': None, 'marital_status': None}], 'primary_member_age': 40, 'sum_insured': 100000, 'pin_code': '751008', 'payment_frequency': 'SINGLE', 'payment_option': None, 'limited_pay': None, 'policy_paying_term': None, 'tobacco_habit': None, 'risk_profile': None, 'maturity_benefit_type': None, 'investment_goal': None, 'investment_amount': None, 'cover_list': None, 'insurance_cover_list': None, 'posp': {'name': 'abcd', 'user_id': '9991112223', 'pan': None, 'aadhar': None, 'level': {'id': 41, 'partner_id': 52, 'role': 'ANAND_RATHI_staff', 'code': 'ANAND_RATHI_POSP', 'superior_role_id': None, 'path': '//41', 'created_at': '2021-06-11T17:48:01.000+05:30', 'updated_at': '2021-06-12T16:42:40.000+05:30'}, 'staf_type': 'POSP'}, 'product_code': 'TEST', 'insurance_code': 'TEST', 'tenure': 1}, 'config': {'coi_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicySchedule', 'password': 'in5uR@nceAnan0', 'plan_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PremiumComputation', 'username': 'ptrn_anandinsurance', 'token_url': 'https://developer.TEST.com/endpoint/token', 'grant_type': 'password', 'policy_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicyGeneration', 'proposal_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/ProposalSave', 'PaymentOption': 'A', 'header_password': 'TEST', 'header_username': 'TEST', 'IntermediaryCode': '2002954741760001', 'UniqueTransactionID': '8765456711111111112232'}}, 'insurance_company_code': 'TEST', 'ic': 'TEST', 'insurance_code': 'TEST', 'product_code': 'TEST', 'category_code': 'TEST', 'callbackUrl': 'https://api.iifl.TEST.com/api_gateway_callbacks/plans'}
balderman
  • 22,927
  • 7
  • 34
  • 52
0

The issue here seems to be the way Python converts dictionaries to strings. Python by default uses single quotes to enclose strings in dictionaries when converted to strings by str().

import json
data = {"foo":"bar"}
str(data)
#"{'foo': 'bar'}"
json.loads(str(data))
# raises json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

This becomes problematic for json.loads() as single quotes are not acceptable JSON format. Also refer to this SO question: Python/Json:Expecting property name enclosed in double quotes

Instead, you should use json.dumps(), which serialises Python objects into JSON formatted string:

import json
data = {"foo":"bar"}
json.dumps(data)
#'{"foo": "bar"}'
json.loads(json.dumps(data))
#{'foo': 'bar'}
Dharman
  • 30,962
  • 25
  • 85
  • 135