Python Forum
Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil
#1
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
Reply
#2
This sounds fairly trivial. Can you show us what you've tried, and explain exactly where you're getting blocked?
Reply
#3
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,
     }))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 66 Yesterday, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 357 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 589 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Parsing large JSON josvink66 5 561 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 669 Nov-14-2023, 11:34 PM
Last Post: snippsat
  format json outputs ! evilcode1 3 1,692 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  JSON Dump and JSON Load foxholenoob 8 978 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  Add two resultant fields from python lambda function klllmmm 4 852 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  TypeRoor reading json GreenLynx 3 800 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,346 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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