Groovy web console

subscribe to the feed Subscribe
to this
site

Lesson15

Published 2 weeks ago by Nikol
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": "AVAILABLE"
    },
    {
        "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": "SOLD"
    },
    {
        "id": 23453523453526222,
        "category": {
            "id": 3,
            "name": "domestic pet"
        },
        "name": "Cat",
        "photoUrls": [
            "https://media.nature.com/lw800/magazine-assets/d41586-020-01443-0/d41586-020-01443-0_17985512.jpg",
            "https://cs4.pikabu.ru/post_img/big/2014/05/21/5/1400654986_1651578960.jpg"
        ],
        "tags": [
            {
                "id": 1,
                "name": "zoo"
            }
        ],
        "status": "SOLD"
    }
]
'''

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

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

print theNode