JSONBuilder in Groovy adds wrong quotation marks - json

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
}
}

Related

how can I insert variables into json file when using terraform

this is the module folder structure
the json definition file (machine_definition.json)
{
"Comment": "A Hello World example of the Amazon States Language using Pass states",
"StartAt": "Hello",
"States": {
"Hello": {
"Type": "Pass",
"Result": "Hello",
"Next": "World"
},
"World": {
"Type": "${var.test}",
"Result": "World",
"End": true
}
}
}
for example I'm trying to enter var.test in here.
how to make the json file detect my variables?
here is the step function definition
module "step-functions" {
source = "terraform-aws-modules/step-functions/aws"
name = "env-${var.environment}-state-machine"
definition = file("${path.module}/machine_definition.json")
tags = var.tags
service_integrations = {
xray = {
xray = true
}
}
cloudwatch_log_group_name = "env-${var.environment}-state-machine-logGroup"
attach_policies = true
number_of_policies = 2
policies = ["arn:aws:iam::aws:policy/AmazonS3FullAccess", "arn:aws:iam::aws:policy/AWSLambda_FullAccess"]
}
Variables cannot be added to a file that way. In order to achieve what you want, you need to use the templatefile [1] built-in fuction. To achieve this you need a bit of code change:
definition = templatefile("${path.module}/machine_definition.json", {
type = var.test
})
Then, in the JSON file, you need to reference the templated variable (type) like this:
{
"Comment": "A Hello World example of the Amazon States Language using Pass states",
"StartAt": "Hello",
"States": {
"Hello": {
"Type": "Pass",
"Result": "Hello",
"Next": "World"
},
"World": {
"Type": "${type}",
"Result": "World",
"End": true
}
}
}
This should render the file properly.
[1] https://developer.hashicorp.com/terraform/language/functions/templatefile

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.

Parsing json in aws lambda function and get elements of 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']))

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
}

how to print json array in jsonobject when we will get element of jsonarrray at runtime

ArrayList al = new ArrayList();
//al[0] and al[1] are the json objects.
def json = new JsonBuilder()
json {
type "rel12"
total k
xyz ""
shows al[0],al[1]
emails ""
}
println json.toPrettyString()
i dont want to do the the hardcoding like al[0]. al[1].
but i need the output like
{
"type": "rel12",
"total": 2,
"xyz": "",
"shows": [
{
"extension": "zip",
"updateTime": 1477521104511
},
{
"extension": "zip",
"updateTime": 1477521104623
}
],
"emails": ""
}