0

I have a .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 each value once from the JSON array how to do this using python which library is best and fast for this??

Update on the Question

I want to update my main.json file like below for each iteration

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": ""
        },
        "status":"success"
    },
    {
        "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": ""
        },
        "status":"success"
    }
]

we have to save status = success in each dict as parameter value if it a success and fail if it fails in the same main.json file.

mycode.py

import json
from numpy import equal 
import requests
new_path = 'new.json'
with open(new_path, 'r') as f:
    data = json.load(f)
    for line in data:
        
        r = requests.post('https://apiuat************', json=line)
        line['status'] = 'success'
        print(r.json())
  • 1
    You can loop over the list and get the dict and pass it using `request.post`. this might help https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests – Abhishek Jan 28 '22 at 10:33

2 Answers2

0

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)
Frederick
  • 450
  • 4
  • 22
0

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)

Abhishek
  • 423
  • 6
  • 12
  • Traceback (most recent call last): File "f:\mohan workspace\json\req_api.py", line 4, in with open('F:\\mis\\mohan workspace\\json\\new.json') as f: FileNotFoundError: [Errno 2] No such file or directory: 'F:\\mis\\mohan workspace\\json\\new.json' I'M getting this error –  Jan 28 '22 at 11:05
  • error clearly mentioned that the file you are trying to read is not present, first check file present or not before reading – Abhishek Jan 28 '22 at 11:28
  • i need to print the response also –  Jan 28 '22 at 11:57
  • To print the response use `print(r.text)` or `print(r.content)` or if you sure that response is in json format use `print(r.json())` – Abhishek Jan 28 '22 at 12:01
  • if this process is completed how can we add the 'status = success' in value in the same 'json array' –  Jan 28 '22 at 13:31
  • if all goes ok you can do `line['status'] = 'success'` to add the status key in particular dict with success as value. – Abhishek Jan 28 '22 at 14:04
  • @Abhisshek how to save status = success in each dict as parameter value if it success and fail if it fails in the same `main.json` file –  Jan 29 '22 at 13:34
  • after saving the status in json , open a file and use `json.dump` like this `with open('main.json','w') as f: json.dump(,f)` – Abhishek Jan 30 '22 at 15:11