Home > other >  Parsing json in aws lambda function and get elements of json
Parsing json in aws lambda function and get elements of json

Time:04-07

This is my JSON file

{
  "Research": {
    "@xmlns": "http://www.rixml.org/2002/6/RIXML",
    "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "@createDateTime": "2022-03-25T12:18:03.647Z",
    "@language": "eng",
    "@researchID": "ABCDEFG_1194968",
    "@xsi:schemaLocation": "http://www.rixml.org/2002/6/RIXML http://www.rixml.org/newsite/specifications/v20/RIXML2.xsd",
    "Product": {
      "@productID": "ABCDEFG_1194968",
      "StatusInfo": {
        "@currentStatusIndicator": "Yes",
        "@statusDateTime": "2022-03-25T12:17:53.686Z",
        "@statusType": "Published"
      },
      "Source": {
        "Organization": {
          "@primaryIndicator": "Yes",
          "@type": "SellSideFirm",
          "OrganizationID": [
            {
              "@idType": "Multex",
              "#text": "767"
            },
            {
              "@idType": "cr pep",
              "#text": "Americas"
            }
          ],
          "OrganizationName": {
            "@nameType": "Display",
            "#text": "VFGH"
          },
          "PersonGroup": {
            "PersonGroupMember": {
              "@primaryIndicator": "Yes",
              "Person": {
                "@personID": "ABCDEFG12275",
                "ContactInfo": {
                  "@nature": "Business",
                  "Email": "[email protected]"
                }
              }
            }
          }
        }
      },
      "Content": {
        "Title": "Weekly Insights: March 25, 2022",
        "Synopsis": null,
        "Resource": {
          "@language": "eng",
          "@primaryIndicator": "Yes",
          "@resourceID": "ABCDEFG_1194968",
          "MIMEType": "application/pdf",
          "Name": "ABCDEFG_1194968.pdf"
        }
      }
    }
  }
}

I need to to parse JSON and get

  1. researchID ,productID and resourceID

I am trying to read file from s3 and then want to parse and process the JSON .

This is the simple code to start with .

import json
import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):

    
    s3_clientobj = s3.get_object(Bucket='avcdf-bulk-dev', Key='RIXML/josnfile.json')
    s3_clientdata = s3_clientobj['Body'].read().decode('utf-8')
    #print(s3_clientdata)
    print (s3_clientdata[0]['Research'])

Apology in advance as this is very basic question to ask but i am new to python and started with AWS lambda . Please suggest something which does not required any lambda layers in AWS if possible .

Tree view is

enter image description here

CodePudding user response:

Convert into dict and then try

import json
import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):

    
    s3_clientobj = s3.get_object(Bucket='avcdf-bulk-dev', Key='RIXML/josnfile.json')
    s3_clientdata = s3_clientobj['Body'].read().decode('utf-8')
    #print(s3_clientdata)
    clientdata_dict = json.loads(s3_clientdata)
    print(clientdata_dict)
    print(clientdata_dict['Research']['@researchID'])

CodePudding user response:

You can access the above mentioned properties like below,

s3_clientobj = s3.get_object(Bucket='avcdf-bulk-dev', Key='RIXML/josnfile.json')
s3_clientdata = json.loads(s3_clientobj['Body'].read().decode('utf-8'))
print("%s, %s, %s" % (s3_clientdata['Research']['@researchID'], s3_clientdata['Research']['Product']['@productID'], s3_clientdata['Research']['Product']['Content']['Resource']['@resourceID']))
  • Related