Python Forum

Full Version: Python Split json into separate json based on node value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using python I want to split a json file into multiple files based on the "transactionTypeName" within the transacations.details. In each file I want the rest of the details as well starting from careperson to username. Below is the json file. Had to clean up the values. Need help with the code suggestion/direction. Thanks in advance.

Learning python I have some idea how to read the json nodes using json.loads. But need help with splitting the json files.
     {
    	"careperson": {
    		"FirstName": "tryy",
    		"LastName": "dbdfb"
    	},
    	"activityDate": "2000-06-14T15:35:00",	
    	"locationAddress": {
    		"Address1": "123g hrtjrtt",
    		"City": "Turrty",
    		"State": "AF",
    		"Zip": "56577"
    	},	
    	"siteName": "Trwtyjj",
    	"transactions": [
    		{
    			"details": [
    				{
    					"expiration": "2002-08-03T23:59:59",
    					"to_sitelocationId": 0
    				}
    			],
    			"transactionType": 6,
    			"transactionTypeName": "Can"
    		},
    		{
    			"details": [
    				{
    					"expiration": "2002-08-03T23:59:59",					
    					"to_sitelocationId": 0
    				}
    			],
    			"transactionType": 6,
    			"transactionTypeName": "Worm"
    		},
    		{
    			"details": [
    				{
    					"expiration": "2002-08-03T23:59:59",
    					"to_sitelocationId": 0
    				}
    			],
    			"transactionType": 6,
    			"transactionTypeName": "Use"
    		}
    	],
    	"sbscrberId": 3344,
    	"sbscrber": "sdg"
    }
I want it to split like this. Basically, "Can", "Worm" and "Use" will be separate files. Below is the expected output for "Worm". "Can" and "Use" will look similar. In this example there are 3 transactionTypes but there can be more for other files so I want to make it dynamic.

  {
    	"careperson": {
    		"FirstName": "tryy",
    		"LastName": "dbdfb"
    	},
    	"activityDate": "2000-06-14T15:35:00",	
    	"locationAddress": {
    		"Address1": "123g hrtjrtt",
    		"City": "Turrty",
    		"State": "AF",
    		"Zip": "56577"
    	},	
    	"siteName": "Trwtyjj",
    	"transactions": [
    		{
    			"details": [
    				{
    					"expiration": "2002-08-03T23:59:59",
    					"to_sitelocationId": 0
    				}
    			],
    			"transactionType": 6,
    			"transactionTypeName": "Worm"
    		}
    	],
    	"sbscrberId": 3344,
    	"sbscrber": "sdg"
    }
if your json data was in a file named 'data.json', you can read the items you want as follows:
I needed the os stuff to let my system know data file was in same directory as the python script.
You mignt be able to eliminate
import json
import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))
with open('data.json') as fp:
    datadict = json.load(fp)
for item in datadict["transactions"]:
    print(item)
Output:
{'details': [{'expiration': '2002-08-03T23:59:59', 'to_sitelocationId': 0}], 'transactionType': 6, 'transactionTypeName': 'Can'} {'details': [{'expiration': '2002-08-03T23:59:59', 'to_sitelocationId': 0}], 'transactionType': 6, 'transactionTypeName': 'Worm'} {'details': [{'expiration': '2002-08-03T23:59:59', 'to_sitelocationId': 0}], 'transactionType': 6, 'transactionTypeName': 'Use'}