Python Forum
Python Split json into separate json based on node value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Split json into separate json based on node value
#1
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"
    }
Reply
#2
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'}
CzarR likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 229 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 451 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 709 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Parsing large JSON josvink66 5 671 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 768 Nov-14-2023, 11:34 PM
Last Post: snippsat
  format json outputs ! evilcode1 3 1,760 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  JSON Dump and JSON Load foxholenoob 8 1,127 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  TypeRoor reading json GreenLynx 3 873 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,545 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,150 Apr-06-2023, 11:15 AM
Last Post: AlphaInc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020