0

The below input is the JSON Array. I am passing the below data to my Python method.

input={"details": [{"first": [
                         {"id": "111", "flag": "T", "planName": "PPO"},
                         {"id": "123", "flag": "F", "planName": "HMO"},
                         {"id": "133", "flag": "T", "planName": "MA"}],
         "second": [{"planName": "PPO", "planRank": "3"},
                     {"planName": "HMO", "planRank": "4"},
                     {"planName": "MA", "planRank": "7"}]}]}

The below is the JSON existing file,

final.json
    {"first": [{"id": "111", "flag": "T", "planName": "EPO"},
                {"id": "133", "flag": "T", "planName": "HMO"},
                {"id": "123", "flag": "T", "planName": "MA"}],
    "second": [{"planName": "PPO", "planRank": "1"},
                {"planName": "HMO", "planRank": "1"},
                {"planName": "MA", "planRank": "1"}]}

When I pass the above input data to my Python method the corresponding fields in the existing JSON files should get updated.From my input JSON when it matches with existing JSON of id then 'flag' and 'planname' should be updated with the passing values from input JSON.

and from second when it matches with 'planName' then PlanRank should be updated with new values passing from the input.

Expected Output(Existing JSON file should get the update like below):

final.json

    {"first": [{"id": "111", "flag": "T", "planName": "PPO"},
                {"id": "133", "flag": "T", "planName": "MA"},
                {"id": "123", "flag": "F", "planName": "HMO"}],
    "second": [{"planName": "PPO", "planRank": "3"},
               {"planName": "HMO", "planRank": "4"},
               {"planName": "MA", "planRank": "7"}]}

I have tried the below code but no luck.

def update_json():
    input={"details": [{"first": [
                             {"id": "111", "flag": "T", "planName": "PPO"},
                             {"id": "123", "flag": "F", "planName": "HMO"},
                             {"id": "133", "flag": "T", "planName": "MA"}],
             "second": [{"planName": "PPO", "planRank": "3"},
                         {"planName": "HMO", "planRank": "4"},
                         {"planName": "MA", "planRank": "7"}]}]}

    for x in input:

        property = open("final.json")
        data = json.load(property)
        for y in range(len(data)):
        for o in range(len(data['first'])):
            if (data['first'][o]['id'] == input[x][0]['id']):
                data['first'][o]['planName'] = input[x][0]['planName']
                data['first'][o]['flag'] = input[x][0]['flag']

        for j in range(len(data['second'])):
            print(data['details'][j])
            if (data['second'][j]['planName'] == input[x][0]['planName']):
                data['second'][j]['planRank'] = input[x][0]['planRank']
    with open(("final.json"), 'w') as file:

        value = json.dumps(data)
        file.write(value)

    return "value"
2
  • what about input['details'][0]? Commented Aug 9, 2018 at 17:57
  • It is not clear where your data object is being loaded. It looks like you are saving the data to a file after you update it and then return a string that just says "value". Also, the input array is nested under the details key, so you may want to actually iterate through for x in input['details']: Commented Aug 9, 2018 at 17:58

1 Answer 1

1

You really shouldn't store anything to variable input as input is a builtin python function.

I tried my best to maintain your coding style.

This works for the above example:

def update_json():
    input_data = {"details": [{"first": [
                             {"id": "111", "flag": "T", "planName": "PPO"},
                             {"id": "123", "flag": "F", "planName": "HMO"},
                             {"id": "133", "flag": "T", "planName": "MA"}],
             "second": [{"planName": "PPO", "planRank": "3"},
                         {"planName": "HMO", "planRank": "4"},
                         {"planName": "MA", "planRank": "7"}]}]}

    with open('final.json') as f:
        data = json.load(f)

    for x in input_data['details'][0]['first']:
        for y in range(len(data['first'])):
            if x['id'] == data['first'][y]['id']:
                data['first'][y]['planName'] = x['planName']
                data['first'][y]['flag'] = x['flag']

    for j in input_data['details'][0]['second']:
        for k in range(len(data['second'])):
            if j['planName'] == data['second'][k]['planName']:
                data['second'][k]['planRank'] = j['planRank']

    with open(("final.json"), 'w') as file:
        value = json.dumps(data)
        file.write(value)

    return value

update_json()
Sign up to request clarification or add additional context in comments.

2 Comments

If I may ask being a student of python, why are your returning the value in the form "value" should it not be value so it can be used by a calling code?
@GeorgeUdosen I agree, it should be value and not "value". I forgot to update that when I posted this. Updating now. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.