Groovy web console

subscribe to the feed Subscribe
to this
site

Lesson15

Published 2 weeks ago by Mykola
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.*
def text = '''[    {
        "id": 23453523453526014,
        "category": {
            "id": 0,
            "name": "test"
        },
        "name": "pet1",
        "photoUrls": [
            "url1",
            "url2"
        ],
        "tags": [
            {
                "id": 0,
                "name": "animals"
            }
        ],
        "status": "SOLD"
    },
    {
        "id": 23453523453526033,
        "category": {
            "id": 0
        },
        "name": "Cat",
        "photoUrls": [
            "url1",
            "url2"
        ],
        "tags": [
            {
                "id": 0,
                "name": "some tag"
            }
        ],
        "status": "AVAILABLE"
    },
    {
        "id": 23453523453526036,
        "category": {
            "id": 0
        },
        "name": "Cat",
        "photoUrls": [
            "url1",
            "url2"
        ],
        "tags": [
            {
                "id": 0,
                "name": "some tag"
            }
        ],
        "status": "AVAILABLE"
    },
    {
        "id": 23453523453526225,
        "category": {
            "id": 0,
            "name": "zoo"
        },
        "name": "pet",
        "photoUrls": [
            "url1",
            "url2"
        ],
        "tags": [
            {
                "id": 0,
                "name": "animals"
            }
        ],
        "status": "SOLD"
    }
]
'''

def json = new JsonSlurper().parseText(text)

def theNode = json.find { it ->it.status = "SOLD"}.id

print theNode