1

I am trying to break this JSON file into two parts

1st Part: The Header Part which contains all the information present before "line:10" or "in_network" Key

2nd Part: The detail Part which contains all the information present inside the key "in_network"

Here is what JSON file looks like

{
  "reporting_entity_name": "launcher",
  "reporting_entity_type": "launcher",
  "plan_name": "launched",
  "plan_id_type": "hios",
  "plan_id": "1111111111",
  "plan_market_type": "individual",
  "last_updated_on": "2020-08-27",
  "version": "1.0.0",
  "in_network": [
    {
      "negotiation_arrangement": "ffs",
      "name": "Boosters",
      "billing_code_type": "CPT",
      "billing_code_type_version": "2020",
      "billing_code": "27447",
      "description": "Boosters On Demand",
      "negotiated_rates": [
        {
          "provider_groups": [
            {
              "npi": [
                0
              ],
              "tin": {
                "type": "ein",
                "value": "11-1111111"
              }
            }
          ],
          "negotiated_prices": [
            {
              "negotiated_type": "negotiated",
              "negotiated_rate": 123.45,
              "expiration_date": "2022-01-01",
              "billing_class": "organizational"
            }
          ]
        }
      ]
    }
  ]
}

Here is my python code which i am trying to use to this operation:

flag = 0
file = open('new_test.JSON', 'r')
detail_file_1 = open('new_test_detail.json', 'a')
detail_file_1.write('{')
detail_file_1.close()
for line in file:
    if flag == 0:
        if line != '  "in_network": [':
            header_file = open('new_test_header.json', 'a')
            header_file.write(line)
            header_file.close()
        else:
            flag = 1
    else:
        detail_file = open('new_test_detail.json', 'a')
        detail_file.write(line)
        detail_file.close()
header_file_1 = open('new_test_header.json', 'a')
header_file_1.write('}')
header_file_1.close()

Here is what i expected 1st part file should look like:

{
  "reporting_entity_name": "launcher",
  "reporting_entity_type": "launcher",
  "plan_name": "launched",
  "plan_id_type": "hios",
  "plan_id": "1111111111",
  "plan_market_type": "individual",
  "last_updated_on": "2020-08-27",
  "version": "1.0.0",
}

Here is what i expected 2nd part file should look like:

"in_network": [
    {
      "negotiation_arrangement": "ffs",
      "name": "Boosters",
      "billing_code_type": "CPT",
      "billing_code_type_version": "2020",
      "billing_code": "27447",
      "description": "Boosters On Demand",
      "negotiated_rates": [
        {
          "provider_groups": [
            {
              "npi": [
                0
              ],
              "tin": {
                "type": "ein",
                "value": "11-1111111"
              }
            }
          ],
          "negotiated_prices": [
            {
              "negotiated_type": "negotiated",
              "negotiated_rate": 123.45,
              "expiration_date": "2022-01-01",
              "billing_class": "organizational"
            }
          ]
        }
      ]
    }
  ]
}

But Unfortunately my code fails to do so. Can some help me with this.

What python code changes is needed to do so.

3
  • "But Unfortunately my code fails to do so." What happens when you try your code? How is that different from the expected result? Also: did you try to look for existing ways to solve the problem, for example by using a search engine? Commented Jan 13, 2023 at 11:05
  • easier way would be to: Read the json into a dict, make copies of the dict, remove the keys that you dont want in those dicts, and write those dicts to a separate files Commented Jan 13, 2023 at 11:05
  • that's not a correct way of diving a json file: you need to access the needed keys and put the part somewhere Commented Jan 13, 2023 at 11:06

1 Answer 1

1

In order to divide this JSON into two smaller JSONs I would do the following:

  • Read the JSON as a Python dictionary
  • Slice the dictionary to two smaller ones using itertools.islice
  • dump the JSONs into two different .json files.

As such:

import json
import itertools


with open('test.json', 'r') as fp:
    data = json.loads(fp.read())


d1 = dict(itertools.islice(data.items(), 8))
d2 = dict(itertools.islice(data.items(), 8, len(data.items())))

# dump these python dictionaries to .json files here

Output:

First part:

{
  "reporting_entity_name": "launcher",
  "reporting_entity_type": "launcher",
  "plan_name": "launched",
  "plan_id_type": "hios",
  "plan_id": "1111111111",
  "plan_market_type": "individual",
  "last_updated_on": "2020-08-27",
  "version": "1.0.0"
}

Second part:

{
  "in_network": [
    {
      "negotiation_arrangement": "ffs",
      "name": "Boosters",
      "billing_code_type": "CPT",
      "billing_code_type_version": "2020",
      "billing_code": "27447",
      "description": "Boosters On Demand",
      "negotiated_rates": [
        {
          "provider_groups": [
            {
              "npi": [
                0
              ],
              "tin": {
                "type": "ein",
                "value": "11-1111111"
              }
            }
          ],
          "negotiated_prices": [
            {
              "negotiated_type": "negotiated",
              "negotiated_rate": 123.45,
              "expiration_date": "2022-01-01", "billing_class": "organizational"
      }
          ]
        }
      ]
    }
  ]
}

Now, if it is dynamic and you don't know whether it is at index 8 or not - you can get it using list(dict.keys()).index:

print(list(data.keys()).index('version'))

I've added one to the stop parameter of islice, because python always goes from 0 to stop-1.

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

Comments

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.