Python Forum

Full Version: Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have around 4 JSON file placed on S3 bucket I need to read those files parse it and then load those data appended into a single JSON file which is placed on S3 bucket can some one help me write a sample code in python for this requirement
This sounds fairly trivial. Can you show us what you've tried, and explain exactly where you're getting blocked?
here is the code i have written: this code simple overwrites the previous value. Can you help

#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3
import json
import decimal

s3_client = boto3.client('s3')
s3 = boto3.resource('s3')

def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    json_file_name = event['Records'][0]['s3']['object']['key']
    print(bucket)
    print(json_file_name)
    json_object = s3_client.get_object(Bucket=bucket,Key = json_file_name)
    jsonFileReader = json_object['Body'].read()
    jsonDict = json.loads(jsonFileReader)
    object = s3.Object('anand-fhir-json', 'Patient_Resource/patient.json')
    for patient in jsonDict:
        try:
            id = str(jsonDict['id'])
        except:
            id = 'null'
        try:
            resourceType = str(jsonDict['resourceType'])
        except:
            resourceType = 'null'
        try:
            identifier = str(jsonDict['identifier'][0]['value'])
        except:
            identifier = 'null'
        try:
            active = str(jsonDict['active'])
        except:
            active = 'null'
        try:
            firstname = str(jsonDict['name'][0]['family'])
        except:
            firstname = 'null'
        try:
            lastname = str(jsonDict['name'][0]['given'][0])
        except:
            lastname = 'null'
        try:
            gender = str(jsonDict['gender'])
        except:
            gender = 'null'
        try:
            managingOrganization_ref = str(jsonDict['managingOrganization']['reference'])
        except:
            managingOrganization_ref = 'null'
        try:
            managingOrganization_display = str(jsonDict['managingOrganization']['display'])
        except:
            managingOrganization_display = 'null'
            
    
    #for patient in object:
    object.put(Body=json.dumps({
    'id': id,
    'resourceType': resourceType,
    'identifier': identifier,
    'active': active,
    'firstname': firstname,
    'lastname': lastname,
    'gender': gender,
    'managingOrganization_ref': managingOrganization_ref,
    'managingOrganization_display': managingOrganization_display,
     }))