jsonbuilder

Exclude null values using JSONBuilder in Groovy

一笑奈何 提交于 2021-01-22 13:56:39
问题 Is it possible to create JSON values in Groovy using the default JsonBuilder library to exclude all the null values of an object? Such as what Jackson does in Java by annotating classes to exclude null values. An example would be: { "userId": "25", "givenName": "John", "familyName": null, "created": 1360080426303 } Which should be printed as: { "userId": "25", "givenName": "John", "created": 1360080426303 } 回答1: Not sure if it's OK for you as my method works on a Map with List properties: def

How to construct json using JsonBuilder with key having the name of a variable and value having its value?

感情迁移 提交于 2020-01-01 08:17:10
问题 How to construct json using JsonBuilder with key and value having same name? import groovy.json.JsonBuilder def userId = 12 // some user id obtained from else where. def json = new JsonBuilder() def root = json { userId userId } print json.toString() Which produces the error groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (java.lang.Integer) values: [12] Possible solutions: wait(), any(), abs(), wait(long), wait(long, int)

modifying json with jsonbuilder in Groovy

落花浮王杯 提交于 2019-12-30 06:12:34
问题 I am trying to modify content of json and then print it to see if it has changed with this code but getting error def builder = new JsonBuilder(request) log.info(builder.content) builder.content.device.dpidsha1= 'abcd' log.info(builder.toPrettyString()) error: no such property: device json looks like this: { "app":{ }, "at":2, "badv":[ ], "bcat":[ ], "device":{ "carrier":"310-410", "connectiontype":3, "devicetype":1, "dnt":0, "dpidmd5":"268d403db34e32c45869bb1401247af9", "dpidsha1":"1234", .

modifying json with jsonbuilder in Groovy

拟墨画扇 提交于 2019-12-30 06:12:30
问题 I am trying to modify content of json and then print it to see if it has changed with this code but getting error def builder = new JsonBuilder(request) log.info(builder.content) builder.content.device.dpidsha1= 'abcd' log.info(builder.toPrettyString()) error: no such property: device json looks like this: { "app":{ }, "at":2, "badv":[ ], "bcat":[ ], "device":{ "carrier":"310-410", "connectiontype":3, "devicetype":1, "dnt":0, "dpidmd5":"268d403db34e32c45869bb1401247af9", "dpidsha1":"1234", .

How to get key from ArrayList nested in JSON using Groovy and change its value

拟墨画扇 提交于 2019-12-19 11:34:42
问题 I need to be able to find the key quote.orderAttributes[0].attributeDetail.name and set its value to null or any other value I want. I only need to do this for the first element in any list so selecting [0] is fine. I want to be able to use a path such as 'quote.orderAttributes.attributeDetail.name'. But given the amount of time I've spent so far, please advise of any better approaches. Here is the Json: { "source": "source", "orderId": null, "Version": null, "quote": { "globalTransactionId":

How to modify JSON in groovy

Deadly 提交于 2019-12-11 03:44:55
问题 I use JsonBuilder to build a JSONObject/String. But, how can I update/change value of one field in this JSONObject/String? I am not seeing the possibility of doing this using JsonBuilder. What show I use then? 回答1: If you have to change the content you already put into the JsonBuilder, then you could do: import groovy.json.* def map = [ users:[ [ name:'tim', posts:43 ], [ name:'alice', posts:72 ] ] ] JsonBuilder builder = new JsonBuilder( map ) builder.content.users[ 0 ].name = 'dave' assert

Setting delegate value with Groovy JsonBuilder

有些话、适合烂在心里 提交于 2019-12-08 01:46:46
问题 (This is a follow up question to a question asked here) I'm using Groovy's JsonBuilder to dynamically generate the following JSON: { "type": { "__type": "urn", "value": "myCustomValue1" }, "urn": { "__type": "urn", "value": "myCustomValue2" }, "date": { "epoch": 1265662800000, "str": "2010-02-08T21:00:00Z" }, "metadata": [{ "ratings": [{ "rating": "NR", "scheme": "eirin", "_type": { "__type": "urn", "value": "myCustomValue3" } }], "creators": [Jim, Bob, Joe] }] } Using this code: def addUrn

How to construct json using JsonBuilder with key having the name of a variable and value having its value?

狂风中的少年 提交于 2019-12-04 00:51:10
How to construct json using JsonBuilder with key and value having same name? import groovy.json.JsonBuilder def userId = 12 // some user id obtained from else where. def json = new JsonBuilder() def root = json { userId userId } print json.toString() Which produces the error groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (java.lang.Integer) values: [12] Possible solutions: wait(), any(), abs(), wait(long), wait(long, int), and(java.lang.Number) Quoting the key does has no effect. Any idea how to make this work. Edit: I

How to get key from ArrayList nested in JSON using Groovy and change its value

霸气de小男生 提交于 2019-12-01 12:53:04
I need to be able to find the key quote.orderAttributes[0].attributeDetail.name and set its value to null or any other value I want. I only need to do this for the first element in any list so selecting [0] is fine. I want to be able to use a path such as 'quote.orderAttributes.attributeDetail.name'. But given the amount of time I've spent so far, please advise of any better approaches. Here is the Json: { "source": "source", "orderId": null, "Version": null, "quote": { "globalTransactionId": "k2o4-6969-1fie-poef", "quoteStatus": "Not Uploaded", "events": { "eventDescription": "event

modifying json with jsonbuilder in Groovy

穿精又带淫゛_ 提交于 2019-11-30 18:51:11
I am trying to modify content of json and then print it to see if it has changed with this code but getting error def builder = new JsonBuilder(request) log.info(builder.content) builder.content.device.dpidsha1= 'abcd' log.info(builder.toPrettyString()) error: no such property: device json looks like this: { "app":{ }, "at":2, "badv":[ ], "bcat":[ ], "device":{ "carrier":"310-410", "connectiontype":3, "devicetype":1, "dnt":0, "dpidmd5":"268d403db34e32c45869bb1401247af9", "dpidsha1":"1234", . . } can someone help in understanding what am i doing wrong and how can i correct it. You need to parse