1

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

2 Answers 2

1

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'])
Sign up to request clarification or add additional context in comments.

Comments

0

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']))

7 Comments

when i do such thing it says "errorMessage": "string indices must be integers",
is it array or dict
if it is an array then you should call like s3_clientdata[0]
Even with this also array i am getting same error for next element .If i provide everywhere position then no error but it returns null
I have tree view of my xml
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.