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.
CodePudding user response:
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
.