Last active
January 17, 2021 23:52
-
-
Save Joxebus/d6a8aac263bcb9c157767a64c3296382 to your computer and use it in GitHub Desktop.
Working with JSON on Groovy - Here are some examples about how to work with JSON objects in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
String json = '{ "name" : "Omar", "lastname": "Bautista", "age" : 33 }' | |
def object = new JsonSlurper().parseText(json) | |
assert object instanceof Map | |
assert object.name == 'Omar' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
String json = '''[ | |
{ "name" : "Omar", "lastname": "Bautista", "age" : 33 }, | |
{ "name" : "Jorge", "lastname": "Valenzuela", "age" : 29 }, | |
{ "name" : "Maria", "lastname": "Ojeda", "age" : 30 } | |
]''' | |
def object = new JsonSlurper().parseText(json) | |
assert object instanceof List | |
assert object[0].name == 'Omar' | |
assert object*.name == ['Omar', 'Jorge', 'Maria'] | |
assert (object*.lastname).contains('Valenzuela') | |
assert object*.age == [33, 29, 30] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
import groovy.json.JsonOutput | |
import groovy.json.JsonGenerator | |
String json = '''{ | |
"status": "Fail", | |
"code": "500", | |
"rules": [{ | |
"status": "Fail", | |
"message": "Code error", | |
"id": "123456" | |
}, | |
{ | |
"status": "Fail", | |
"message": "Configuration error", | |
"id": "12345" | |
} | |
] | |
}''' | |
Map input = new JsonSlurper().parseText(json) | |
def generator = new JsonGenerator.Options() | |
.excludeFieldsByName('id', 'code') | |
.build() | |
String output = generator.toJson(input) | |
println JsonOutput.prettyPrint(output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.* | |
URL feedJSON = 'https://refind.com/chrismessina.json?amount=10'.toURL() | |
def feed = new JsonSlurper().parseText(feedJSON.text) | |
def formatDate = { stringDate -> | |
Date date = new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", stringDate) | |
date.format("dd/MM/yyyy HH:mm") | |
} | |
feed.each { entry -> | |
println """ | |
title: ${entry.title} | |
url: ${entry.url} | |
created: ${formatDate(entry.created_at)} | |
""" | |
} | |
assert feed.size() == 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment