Parsing json in aws lambda function and get elements of json - json

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": "su.ppe#cr-pep.com"
}
}
}
}
}
},
"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
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

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

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

Preparing JSON array of Objects from multiple array list

I am very new to the Groovy scripts and would like to build a JSON output from the below JSON input. Kindly help!
My JSON input looks like this:
{
"id":"1222",
"storageNode": {
"uuid": "22255566336",
"properties": {
"BuinessUnit": [
"Light",
"Fan",
"Watch"
],
"Contact": [
"abc#gmail.com",
"fhh#gmail.com"
],
"Location": [
"Banglore",
"Surat",
"Pune"
]
}
}
}
Expected Output:
[
{
"BuinessUnit": "Light",
"Contact": "abc#gmail.com",
"Location": "Banglore"
},
{
"BuinessUnit": "Fan",
"Contact": "fhh#gmail.com",
"Location": "Surat"
},
{
"BuinessUnit": "Watch",
"Contact": "",
"Location": "Pune"
}
]
Please note that in case any array is not matching the value count that will always be the last one and in that case, a blank value ("") has to be populated. The "BusinessUnit" object can be referred for array size validation.
My code looks like this:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;
def Message processData(Message message) {
//Body
def body = message.getBody(String.class);
def jsonSlurper = new JsonSlurper()
def list = jsonSlurper.parseText(body)
String temp
def BU = list.storageNode.properties.get("BusinessUnit")
def builder = new JsonBuilder(
BU.collect {
[
BusinessUnit: it
]
}
)
message.setBody(builder.toPrettyString())
return message
}
It is only returning this:
[
{
"BusinessUnit": "Light"
},
{
"BusinessUnit": "Fan"
},
{
"BusinessUnit": "Watch"
}
]
Now how will I add other parts to it? Please help!
I have come up with the following solution that converts source JSON string to the target JSON string:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def json = '''
{
"id":"1222",
"storageNode": {
"uuid": "22255566336",
"properties": {
"BusinessUnit": [
"Light",
"Fan",
"Watch"
],
"Contact": [
"abc#gmail.com",
"fhh#gmail.com"
],
"Location": [
"Banglore",
"Surat",
"Pune"
]
}
}
}
'''
println convert(json)
String convert(String json) {
def list = new JsonSlurper().parseText(json)
List<String> units = list.storageNode.properties.BusinessUnit
List<String> contacts = list.storageNode.properties.Contact
List<String> locations = list.storageNode.properties.Location
def result = []
units.eachWithIndex { unit, int index ->
result << [
BusinessUnit: unit,
Contact : contacts.size() > index ? contacts[index] : '',
Location : locations.size() > index ? locations[index] : '',
]
}
return new JsonBuilder(result).toPrettyString()
}
I've omitted the logic of getting string from the message and packaging transformed JSON into message.
I hope it will help you to move forward. Please let me know if you need further assistance here.
You can use the built-in Groovy facilities, like transpose():
import groovy.json.*
def json = new JsonSlurper().parseText '''{ "id":"1222", "storageNode": { "uuid": "22255566336", "properties": {
"BuinessUnit": [ "Light", "Fan", "Watch" ],
"Contact": [ "abc#gmail.com", "fhh#gmail.com" ],
"Location": [ "Banglore", "Surat", "Pune" ] } } }'''
def names = json.storageNode.properties*.key
def values = json.storageNode.properties*.value
int maxSize = values*.size().max()
// pad lists with trainiling spaces
values.each{ v -> ( maxSize - v.size() ).times{ v << '' } }
def result = values.transpose().collect{ tuple -> [ names, tuple ].transpose().collectEntries{ it } }
assert result.toString() == '[[BuinessUnit:Light, Contact:abc#gmail.com, Location:Banglore], [BuinessUnit:Fan, Contact:fhh#gmail.com, Location:Surat], [BuinessUnit:Watch, Contact:, Location:Pune]]'
This piece of code can process everything under storageNode.properties.

JSONBuilder in Groovy adds wrong quotation marks

I'm trying to read a json file, edit some parts of it and then parse it back to a json file. The goal is to change the value of a confluence page. I'm using the groovy code in a Jenkins pipeline. Here it is:
def changeValue(){
def json_map = readJSON file: '/tmp/updater.json'
def body_content = '{"storage":{"value":'
body_content += '"<h1>test</h1>"'
body_content += ',"representation":"storage"}}'
json_map.body = body_content
json_as_string = new JsonBuilder(json_map).toPrettyString().replaceAll("\\\\", "") // It also adds unneccesary escapes
print json_as_string
}
This is the contents of the updater.json:
{
"id":"redacted",
"type":"page",
"title":"redacted",
"space":{"key":"redacted"},
"body":{"storage":{"value":"<h1>wrong</h1>","representation":"storage"}},
"version":{
"number":6
}
}
That is what I get:
{
"id": "redacted",
"type": "page",
"title": "redacted",
"space": {
"key": "redacted"
},
"body": "{"storage":{"value":"<h1>test</h1>","representation":"storage"}}",
"version": {
"number": 6
}
}
As you can see, it added quotation marks around the block of the body. How can I get rid of them?
The result is as expected, you update the body with a new String.
If you want to update only the value use this based on this answer
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def jsn = """
{
"id":"redacted",
"type":"page",
"title":"redacted",
"space":{"key":"redacted"},
"body":{"storage":{"value":"<h1>wrong</h1>","representation":"storage"}},
"version":{
"number":6
}
}"""
def slp= new JsonSlurper().parseText(jsn)
bld.content.body.storage.value = '<h1>test</h1>'
println(bld.toPrettyString())
result
{
"id": "redacted",
"type": "page",
"title": "redacted",
"space": {
"key": "redacted"
},
"body": {
"storage": {
"value": "<h1>test</h1>",
"representation": "storage"
}
},
"version": {
"number": 6
}
}

Python accessing element inside array inside json

Can someone help me to access myapp from below json using python, please ?
{
"Data": {
"Name": "myname",
"AccountName": "test",
"classic": [
{
"cName": "mycname",
"tags": ["tag1", "tag2"],
"Version": "1.14.10",
"app": "myapp"
}
]
}
}
Use json.load for reading a file or json.loads for a string.
data_dict = json.loads(your_data)
print(data_dict["Data"]["classic"][0]["app"])
Use json loads to parse a json string and json dumps for stringifying the json to a json string
Here in your case you will need json loads
input = {
"Data": {
"Name": "myname",
"AccountName": "test",
"classic": [
{
"cName": "mycname",
"tags": ["tag1", "tag2"],
"Version": "1.14.10",
"app": "myapp"
}
]
}
}
parsed_input = json.loads(input)
output = parsed_input["Data"]["classic"][0]["app"]
print(output)
You can use json library for the same.
input_json= {
"Data": {
"Name": "myname",
"AccountName": "test",
"classic": [
{
"cName": "mycname",
"tags": ["tag1", "tag2"],
"Version": "1.14.10",
"app": "myapp"
}
]
}
}
data = json.loads(input_json)
result=data["Data"]["classic"][0]["app"])

Json Array to Map using JsonSlurper

I want to create a map of data with the pid value as the key and the name as the array for a json array. Here is the json structure:
{
"measurements": [
{
"pid": 6691,
"name": "lung",
"measurement": "qualityFactor",
},
{
"pid": 1106,
"name": "kidney",
"measurement": "qualityFactor",
},
{
"id": 119,
"name": "pancreas",
"measurement": "qualityFactor",
},
]
}
Here is my attempt with Groovy but I'm stuck:
def jsonSlurper= new JsonSlurper()
Object objs=jsonSlurper.parseText(jsonData)
List pp =objs.data
Map<String,String> m=new HashMap()
pp.each{ it ->
it.collect{Map mm ->
println "Map m is ${mm}"
}
}
I want the map to look like
["6691" : "lung" , "1106" :"kidney" ....] etc.
Hiw can I accomplish this?
As a full answer with corrected JSON:
def json = '''{
"measurements": [
{
"pid": 6691,
"name": "lung",
"measurement": "qualityFactor",
},
{
"pid": 1106,
"name": "kidney",
"measurement": "qualityFactor",
},
{
"pid": 119,
"name": "pancreas",
"measurement": "qualityFactor",
}
]
}
'''
import groovy.json.*
def parsed = new JsonSlurper().parseText(json)
def map = map.measurements.collectEntries { [it.pid.toString(), it.name] }

how to parse the json file and read the json elements in groovy

Hi i am trying to parse the below json file. I tried using jsonsluper and parsed the file. I executed below command. Nothing works.
def test =newjsonslurper().parsetext(organist)
test.resources.each{
println it.resources.metadata. "guid"
println it.resources.entity. "name"
}
This is the json file format
resources: [
{
"metadata" :{
"guid":"cya"
"url": "dummy.test"
},
"entity" :
{
"name": "system"
"status": "active"
}
}
{
"metadata" :
{
"guid":"cya"
"url": "dummy.test"
},
"entity" :
{
"name": "system"
"status": "active"
}
}
]
There were a couple of problems:
JsonSlurper().parseText() expects a String. If you're wanting to parse a file, use something like def response = new JsonSlurper().parse(new File('JsonFile.json'))
The JSON payload is not valid: it's missing a few brackets and commas.
The following code should work:
import groovy.json.JsonSlurper
def test = new JsonSlurper().parseText '''
{"resources": [
{
"metadata": {
"guid": "cya",
"url": "dummy.test"
},
"entity": {
"name": "system",
"status": "active"
}
},
{
"metadata": {
"guid": "cya",
"url": "dummy.test"
},
"entity": {
"name": "system",
"status": "active"
}
}
]}
'''
test.resources.each {
println it.metadata.guid
println it.entity.name
}