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
  Convert Json to table format python_student 4 14,770 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Trying to get JSON object in python and process it further Creepy 2 1,050 Oct-24-2024, 08:46 AM
Last Post: buran
  Write json data to csv Olive 6 1,384 Oct-22-2024, 06:59 AM
Last Post: Olive
  get JSON string from URL with Windows credentials shwfgd 0 659 Aug-27-2024, 10:08 PM
Last Post: shwfgd
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,094 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  Python beginner needs json help rwskinner 1 614 Aug-22-2024, 05:30 PM
Last Post: deanhystad
  Trying to generating multiple json files using python script dzgn989 4 2,373 May-10-2024, 03:09 PM
Last Post: deanhystad
  encrypt data in json file help jacksfrustration 1 2,286 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 1,636 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 4,407 Jan-23-2024, 07:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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