I'm pretty new to Groovy (and json) and being playing around with this code trying to get it to work, almost but not quite getting there and need a little help...
So what I'm trying to do is parse an existing json file and then add/append additional entries as in below example:
Original Json
{
"organisation": "company",
"modules": [
{
"description": "Module 1",
"type": "Q1",
},
{
"description": "Module 2",
"type": "Q2",
},
{
"description": "Module 3",
"type": "Q3",
}
]
}
New Json
modules {
description 'Module 4'
type 'TEST'
}
Intended Final Output
{
"organisation": "company",
"modules": [
{
"description": "Module 1",
"type": "Q1",
},
{
"description": "Module 2",
"type": "Q2",
},
{
"description": "Module 3",
"type": "Q3",
},
{
"description": "Module 4",
"type": "TEST",
}
]
}
I've tried many variations on the following code snippet but still not quite getting the right format for my intended output
def inputFile = file("modules.json")
def outputFile = new File("modules.new.json")
def json = new JsonSlurper().parseText(inputFile.text)
println "This is our original input JsonSlurper: \n"
println JsonOutput.prettyPrint(JsonOutput.toJson (json))
def builder = new JsonBuilder()
def jsonNew = builder {
modules {
description 'Module 4'
type 'TEST'
}
}
println "This is our combined output JsonBuilder: \n"
println JsonOutput.prettyPrint(JsonOutput.toJson ([json, jsonNew]))
Which results in the below:
[
{
"organisation": "company",
"modules": [
{
"description": "Module 1",
"type": "Q1"
},
{
"description": "Module 2",
"type": "Q2"
},
{
"description": "Module 3",
"type": "Q3"
}
]
},
{
"modules": {
"description": "Module 4",
"type": "TEST"
}
}
]
Any help sorting this would be much appreciated.
You need to merge maps before produce json:
json.modules = json.modules << jsonNew.modules
println JsonOutput.prettyPrint(JsonOutput.toJson(json))
[json, jsonNew] this just creates an array of objects, it's not adding the new object into the json.modules array.
Related
I am working with a specific API that returns a JSON that looks like the below sample.
I want to get both values that contain the #text and #attr but I get error messages in typescript when I try to get the values.
try using,
album[0]["#attr"]
album[0]["artist"]["#text"]
Hey for JSON you can use get details by its attribute name in it and it's the same for all-weather it starts with # or # it will be the same.
See below code to get the value of your specified key:
Sample JSON:
{
"weeklyalbumchart": {
"album": [
{
"artist": {
"mbid": "data",
"#text": "Flying Lotus"
},
"mbid": "data",
"url": "",
"name": "",
"#attr": {
"rank": "1"
},
"playcount": "21"
},
{
"artist": {
"mbid": "data",
"#text": "Flying Lotus"
},
"mbid": "data",
"url": "",
"name": "",
"#attr": {
"rank": "1"
},
"playcount": "21"
}
]
}
}
Read JSON:
#attr ===> json["weeklyalbumchart"]["album"][0]["#attr"]
#text ===> json["weeklyalbumchart"]["album"][0]["artist"]["#text"]
Hope this will help you to understand it.
I have a Json below which I am experiencing parse issues. I am not able to write sub array elements inside one array element. Need your help
{
"fulfillmentMessage": [{
"text": {
"text": [
"Element 1",
"Element 2",
"additionalCategory": [{
"link": "",
"followup_text": "Yes",
"title": "Yes"
},
{
"title": "No",
"followup_text": "No",
"link": ""
}
]
]
}
}]
}
For the above JSON, I am getting the below error
Error: Expecting Comma or ], not colon
You missed the curly braces before "additionalCategory".
Try this.
var json = {
"fulfillmentMessage": [{
"text": {
"text": [
"Element 1",
"Element 2",
{
"additionalCategory": [{
"link": "",
"followup_text": "Yes",
"title": "Yes"
},
{
"title": "No",
"followup_text": "No",
"link": ""
}
]
}
]
}
}]
};
console.log(json);
I am working with a JSON file which has contains lot of data that can be removed before sending to an API.
Found that JQ can be used to achieve this but not sure on how to map to get the desired results.
Input JSON
{
"name": "Sample name",
"id": "123",
"userStory": {
"id": "234",
"storyName": "Story Name",
"narrative": "Narrative",
"type": "feature"
},
"testSteps": [
{
"number": 1,
"description": "Step 1",
"level": 0,
"children": [
{
"number": 2,
"description": "Description",
"children": [
{
"number": 3,
"description": "Description"
}
]
},
{
"number": 4,
"anotherfield": "another field"
}
]
}
]
}
Desired Output
{
"name": "Sample name",
"userStory": {
"storyName": "Story Name"
},
"testSteps": [
{
"description": "Step 1",
"children": [
{
"description": "Description",
"children": [
{
"description": "Description"
}
]
},
{
"anotherfield": "anotherfield"
}
]
}
]
}
Tried to do it with the following jq command
map_values(..|{name, id, userStory})
but not sure how to filter only the userStory.storyName.
Thanks in advance.
Note: The actual JSON has different child elements that are repeated in some cases.
To delete .id from root object:
del(.id)
To leave only .storyName in .userStory:
.userStory |= {storyName}
To delete .number and .level from every object on any level in .testSteps:
.testSteps |= walk(if type == "object" then del(.number, .level) else . end)
Putting it all together:
del(.id) | (.userStory |= {storyName}) | (.testSteps |=
walk(if type == "object" then del(.number, .level) else . end))
Online demo
My File looks like this
{
"Item Version": 1.0,
"Item Creation Time": "2019-04-14 14:15:09",
"Trade Dictionary": {
"Country": "India",
"TradeNumber": "1",
"action": {
"Action1": false
},
"Value": "XXXXXXXXXXXXXXX"
},
"Payments": {
"Payment Details": [{
"Payment Date": "2019-04-11",
"Payment Type": "Rej"
}]
}
}
I want the result in below format
Item Version,Item Creation Time,Trade Dictionary.Country,Trade Dictionary.TradeNumber,Trade Dictionary.TradeNumber.action.Action1,Trade Dictionary.TradeNumber.value,Payment.Payment Details.Payment Date,Payment.Payment Details.Payment Type
I'm new to Groovy. But, I can try with some simple code lines to extract the data.
I have read the from Parse JSON using groovy script (using JsonSlurper). But it didn't help in my case.
My code line:
{
"errors": false,
"address_data": [
{
"address_id": "567",
"township": {
"id": "41079",
"name": "Test Data"
},
"city": {
"id": "1622",
"name": "Test City"
},
"region": {
"id": "663",
"name": "Metro Test"
},
"stock_source_code": "Test",
"is_default_address": false
},
{
"address_id": "45444",
"township": {
"id": "41079",
"name": "Test Test"
},
"city": {
"id": "1622",
"name": "Test City"
},
"region": {
"id": "663",
"name": "Metro Test Taguig"
},
"is_default_address": true
},
{
"address_id": "45444",
"township": {
"id": "888888",
"name": "Apas"
},
"city": {
"id": "432",
"name": "Test City"
},
"region": {
"id": "591",
"name": "Test Cebu"
},
"stock_source_code": "testce",
"is_default_address": false
}
]
}
My code lines:
def response = "data_above";
def object = new JsonSlurper().parseText(response);
def errors = object.errors
if (errors == false) { //could access object.errors property
log.info "Checking condition"
def addressData = object.address_data // Cannot get the data
}
How can I get the address_data property?