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. Thanks in advance.

I have some idea how to read the json nodes using json.loads. But no clue on how to split.

 {
    "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"
}
CzarSvk
  • 27
  • 1
  • 6
  • So it has the same header and footer info, but the transactions array will only contain transactions of the relevant type? It looks like you're going to open three file pointers, write the header in, loop through the transactions and only write transactions to the write file, finish off each file, and close them. – Kevin Jul 08 '22 at 15:24
  • Yes, it will have the same header and footer info. There can more than 3 transactiontypes so pointers you were suggesting should be dynamic. Can you please suggest the code? – CzarSvk Jul 08 '22 at 15:28

1 Answers1

0

As Kevin said, looping through each transaction would be best. The process would be:

  1. Loop through each transaction.
  2. Record each key-value pair to a temporary JSON object.
  3. Override the 'transactions' key to include only one transaction object.
  4. Write the temporary object to a file using 'json.dump' Write JSON Object to a file. Then loop over until all transactions have been split.

This could be simplified by copying the JSON read from the file to a temporary object.

In Python 3, it would look like this (Added comments to explain each step):

import json
# Load JSON data from a file into object
fileRead = open('test.json', 'r')
jsonContent = json.load(fileRead)
fileRead.close()

# Loop through each transaction read from the file (3 in the example)
for transaction in jsonContent['transactions']:
    jsonFileContent = {}
    
    # Loop through each json key value pair read from the object.
    for key, value in jsonContent.items():
        if key == 'transactions':
            # Override 'transactions' key with the single value that was generated from the first loop
            # Initialize empty List
            jsonFileContent["transactions"] = []
            # Add Transaction to list
            jsonFileContent["transactions"].append(transaction)
        else:
            # Write key value pair to a temporary object
            jsonFileContent[key] = value
            
    # Write all contents to file a file 'transactionTypeName'.json (e.g. 'can.json')
    fileWrite = open(jsonFileContent['transactions'][0]['transactionTypeName']+'.json', 'w')
    json.dump(jsonFileContent, fileWrite)               
    fileWrite.close()
Patrick
  • 101
  • 4