jsonslurper

How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

筅森魡賤 提交于 2021-01-05 09:13:43
问题 The question is still relevant! In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values ​​using reader.outputLines . Now the list of json field types is formed like this: def types = list.find (). Values ​​() *. GetClass () *. SimpleName But I am faced with a problem when the same field in the first json block is null, and in the second the integer and the type is defined as null, and not as Integer. How to

How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

£可爱£侵袭症+ 提交于 2021-01-05 09:12:00
问题 The question is still relevant! In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values ​​using reader.outputLines . Now the list of json field types is formed like this: def types = list.find (). Values ​​() *. GetClass () *. SimpleName But I am faced with a problem when the same field in the first json block is null, and in the second the integer and the type is defined as null, and not as Integer. How to

Is there any way to list latest Nexus Artifacts in Jenkins?

和自甴很熟 提交于 2020-12-07 08:10:12
问题 I am using Acive Choice Reactive Parameter plugin to list down Nexus Artifacts. This is the groovy script which I'm currently using. import groovy.json.* def targetUrl = "https://nexus.xxxx.lk/service/rest/v1/search?repository=snapshots&format=maven2&group=com.org.pro&name=pro-service" def jsonSlupper = new JsonSlurper().parse(URI.create(targetUrl).toURL()) def list = jsonSlupper["items"]["version"].collect().sort().reverse() I want to display only latest artifact in the list. Does anyone

Groovy: validate JSON string

断了今生、忘了曾经 提交于 2020-07-18 04:09:26
问题 I need to check that a string is valid JSON in Groovy. My first thought was just to send it through new JsonSlurper().parseText(myString) and, if there was no exception, assume it was correct. However, I discovered that Groovy will happily accept trailing commas with JsonSlurper , but JSON doesn't allow trailing commas. Is there a simple way to validate JSON in Groovy that adhere's to the official JSON spec? 回答1: JsonSlurper class uses JsonParser interface implementations (with

Recursively extracting JSON field values in Groovy

时光毁灭记忆、已成空白 提交于 2019-12-29 10:06:27
问题 I need to implement a method that will scan a string of JSON for a particular targetField and either return the value of that field (if it exists), or null (if it doesn't): // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'fizz') => 'buzz' // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'foo') => null String extractFieldValue(String json, String targetField) { // ... } This solution has to be recursive and work at any nesting-level in the (hierarchical) JSON string. Also it needs to work for

Recursively extracting JSON field values in Groovy

浪子不回头ぞ 提交于 2019-12-29 10:05:09
问题 I need to implement a method that will scan a string of JSON for a particular targetField and either return the value of that field (if it exists), or null (if it doesn't): // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'fizz') => 'buzz' // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'foo') => null String extractFieldValue(String json, String targetField) { // ... } This solution has to be recursive and work at any nesting-level in the (hierarchical) JSON string. Also it needs to work for

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":

Groovy JsonSlurper and nested maps

霸气de小男生 提交于 2019-12-08 11:02:09
问题 I have a method that returns fairly-nested JSON such as: [[fizz: buzz, foos: [[count: 4, flim: flam], [count: 6, flim: flume]]]] When I try to use JsonSlurper to slurp this JSON into a def result I am getting exceptions: // json == “[[fizz: buzz, foos: [[count: 4, flim: flam], [count: 6, flim: flume]]]]" String json = getJSON() JsonSlurper slurper = new JsonSlurper() def result = slurper.parseText(json) Produces an exception thrown when parseText executes: Caught: groovy.json.JsonException:

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

Recursively extracting JSON field values in Groovy

独自空忆成欢 提交于 2019-11-29 17:59:48
I need to implement a method that will scan a string of JSON for a particular targetField and either return the value of that field (if it exists), or null (if it doesn't): // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'fizz') => 'buzz' // Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'foo') => null String extractFieldValue(String json, String targetField) { // ... } This solution has to be recursive and work at any nesting-level in the (hierarchical) JSON string. Also it needs to work for JSON arrays as well. My best attempt so far: String extractFieldValue(String json, String targetField) {