Home > Software engineering >  how to push json values to API one by one from JSON array using python
how to push json values to API one by one from JSON array using python

Time:01-29

I have .json file with some JSON data like below,

main.json

[
    {
        "App_id": "",
        "mobile_No": 8****65128,
        "loan_type": 33,
        "bank_id": 114,
        "latest_status": "Rejected",
        "bank_appid": "",
        "pan_no": "",
        "cust_downloadedapp": {
            "date": ""
        },
        "cust_completedapp": {
            "date": ""
        },
        "rejected": {
            "date": "",
            "rejection_reason": ""
        },
        "sanctioned": {
            "amount": "",
            "date": "",
            "tenure": "",
            "interest_rate": ""
        },
        "offer_accepted": {
            "date": ""
        },
        "disbursed": {
            "amount": "",
            "los_id": "",
            "date": ""
        }
    },
    {
        "App_id": "",
        "mobile_No": 70007*****8,
        "loan_type": 33,
        "bank_id": 114,
        "latest_status": "Rejected",
        "bank_appid": "",
        "pan_no": "",
        "cust_downloadedapp": {
            "date": ""
        },
        "cust_completedapp": {
            "date": ""
        },
        "rejected": {
            "date": "",
            "rejection_reason": ""
        },
        "sanctioned": {
            "amount": "",
            "date": "",
            "tenure": "",
            "interest_rate": ""
        },
        "offer_accepted": {
            "date": ""
        },
        "disbursed": {
            "amount": "",
            "los_id": "",
            "date": ""
        }
    },
    {
        "App_id": "",
        "mobile_No": 84*****399,
        "loan_type": 33,
        "bank_id": 114,
        "latest_status": "Rejected",
        "bank_appid": "",
        "pan_no": "",
        "cust_downloadedapp": {
            "date": ""
        },
        "cust_completedapp": {
            "date": ""
        },
        "rejected": {
            "date": "",
            "rejection_reason": ""
        },
        "sanctioned": {
            "amount": "",
            "date": "",
            "tenure": "",
            "interest_rate": ""
        },
        "offer_accepted": {
            "date": ""
        },
        "disbursed": {
            "amount": "",
            "los_id": "",
            "date": ""
        }
    }
]

My issue is how i parse this below code to API And i also want to parse the values one by one

    {
        "App_id": "",
        "mobile_No": 84****0399,
        "loan_type": 33,
        "bank_id": 114,
        "latest_status": "Rejected",
        "bank_appid": "",
        "pan_no": "",
        "cust_downloadedapp": {
            "date": ""
        },
        "cust_completedapp": {
            "date": ""
        },
        "rejected": {
            "date": "",
            "rejection_reason": ""
        },
        "sanctioned": {
            "amount": "",
            "date": "",
            "tenure": "",
            "interest_rate": ""
        },
        "offer_accepted": {
            "date": ""
        },
        "disbursed": {
            "amount": "",
            "los_id": "",
            "date": ""
        }
    }

JSON value to API as post method using python.? And also need to call the each value once from the JSON array how to do this using python which library is best and fast for this??

CodePudding user response:

try

import json 
import requests

with open("main.json") as f:
    data = json.load(f)
    for line in data:
        r = requests.post('URL',json=line)
        print(r.status_code)

CodePudding user response:

You can work with arrays in JSON, like you would work with arrays in python normally. Here is the example:

import json

with open("main.json") as f:
    data = json.load(f)

for item in data:
    requests.post(url, json=data)
  •  Tags:  
  • Related