{
"Product": [{
"flm": [{
"101": [{
"value": "R36.0 flm",
"revision": ["111","11br"]
}]
"101.1": [{
"value": "R36.0 flm",
"revision": ["111","11br","11we"]
}]
"202": [{
"value": "R37.0 flm",
"revision": ["111"]
}]
"301": [{
"value": "R38.0 flm",
"revision": ["222"]
}]
}]
"cod": [{
"101": [{
"value": "R36.0 cod",
"revision": ["111"]
}]
"202": [{
"value": "R37.0 cod",
"revision": ["111"]
}]
"301": [{
"value": "R38.0 cod",
"revision": ["222"]
}]
}]
"lsp": [{
"101": [{
"value": "R36.0 lsp",
"revision": ["111","11br"]
}]
"202": [{
"value": "R37.0 lsp",
"revision": ["111"]
}]
"301": [{
"value": "R38.0 lsp",
"revision": ["222"]
}]
}]
"urm": [{
"101": [{
"value": "R36.0 urm",
"revision": ["111","11br"]
}]
"202": [{
"value": "R37.0 urm",
"revision": ["111"]
}]
"301": [{
"value": "R38.0 urm",
"revision": ["222"]
}]
}]
}]
}
I want to print/access the value/revision fields in the json file using groovy and compare if it matches my input ("11br" ,R36.0 lsp ) if product is (lsp and 101). I can only reach till 101 and later i am not sure how to extract further values. Any help is appreciated
You can use JsonSlurper.parseText(json)
for that - it will produce a map of maps that you can traverse and extract interesting values. In your case it is important to mention that you have to deal with lists of objects (usually they contain only one object), so traversing will produce lists of lists etc. In this case Groovy's .flatten()
method is very helpful. Consider following script:
import groovy.json.JsonSlurper
def json = '''
{
"Product": [
{
"flm": [
{
"101": [
{
"value": "R36.0 flm",
"revision": [
"111",
"11br"
]
}
],
"101.1": [
{
"value": "R36.0 flm",
"revision": [
"111",
"11br",
"11we"
]
}
],
"202": [
{
"value": "R37.0 flm",
"revision": [
"111"
]
}
],
"301": [
{
"value": "R38.0 flm",
"revision": [
"222"
]
}
]
}
],
"cod": [
{
"101": [
{
"value": "R36.0 cod",
"revision": [
"111"
]
}
],
"202": [
{
"value": "R37.0 cod",
"revision": [
"111"
]
}
],
"301": [
{
"value": "R38.0 cod",
"revision": [
"222"
]
}
]
}
],
"lsp": [
{
"101": [
{
"value": "R36.0 lsp",
"revision": [
"111",
"11br"
]
}
],
"202": [
{
"value": "R37.0 lsp",
"revision": [
"111"
]
}
],
"301": [
{
"value": "R38.0 lsp",
"revision": [
"222"
]
}
]
}
],
"urm": [
{
"101": [
{
"value": "R36.0 urm",
"revision": [
"111",
"11br"
]
}
],
"202": [
{
"value": "R37.0 urm",
"revision": [
"111"
]
}
],
"301": [
{
"value": "R38.0 urm",
"revision": [
"222"
]
}
]
}
]
}
]
}
'''
def root = new JsonSlurper().parseText(json)
def lsp101 = root.Product.lsp.'101'.flatten()
def value = lsp101.value.first()
def revisions = lsp101.revision.flatten()
def expectedValue = 'R36.0 lsp'
def expectedRevision = '11br'
assert expectedValue == value && expectedRevision in revisions
It extracts value
and a list of revisions
for path $.Product.lsp.101
and compares with expected values. Hope it helps understanding how can you work with JSON in Groovy. For more information and examples don't hesitate to visit Groovy manual page that describes how to work with JSON in details - http://groovy-lang.org/json.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.