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
  Errors using json file RonR 5 955 Oct-28-2025, 03:00 PM
Last Post: buran
  Convert JSON to CSV Linuxdesire 6 8,022 Oct-15-2025, 12:05 AM
Last Post: Pedroski55
  How to optimize the speed of processing large JSON files in Python without using too sophia2005 3 963 Aug-02-2025, 03:25 PM
Last Post: snippsat
  Convert Json to table formathttps://python-forum.io/thread-38313.html python_student 3 18,705 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Trying to get JSON object in python and process it further Creepy 2 1,582 Oct-24-2024, 08:46 AM
Last Post: buran
  Write json data to csv Olive 6 2,436 Oct-22-2024, 06:59 AM
Last Post: Olive
  get JSON string from URL with Windows credentials shwfgd 0 1,097 Aug-27-2024, 10:08 PM
Last Post: shwfgd
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,250 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Python beginner needs json help rwskinner 1 1,095 Aug-22-2024, 05:30 PM
Last Post: deanhystad
  Trying to generating multiple json files using python script dzgn989 4 4,664 May-10-2024, 03:09 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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