Groovy удалить с карты - PullRequest
       4

Groovy удалить с карты

0 голосов
/ 05 февраля 2019

Список ниже содержит коллекцию id

List id_wanted = ['3894586', '2786438236', '895673985']

Учитывая приведенный выше список, Как удалить элементы из JSON ниже, которые соответствуют идентификатору выше List?

JSON:

{
    "animals": [
        {
            "name": "lion",
            "countries": [
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "young male"
                            ],
                            "age": "2y",
                            "id": "2837492"
                        }
                    ]
                },
                {
                    "name": "tanzania",
                    "facts": [
                        {
                            "features": [
                                "cub"
                            ],
                            "age": "0y",
                            "id": "3894586"
                        }
                    ]
                },
                {
                    "name": "south africa",
                    "facts": [
                        {
                            "features": [
                                "adult lioness"
                            ],
                            "age": "10y",
                            "id": "495684576"
                        },
                        {
                            "features": [
                                "young female"
                            ],
                            "age": "4y",
                            "id": "2786438236"
                        }
                    ]
                }
            ]
        },
        {
            "name": "giraffe",
            "countries": [
                {
                    "name": "zambia",
                    "facts": [
                        {
                            "features": [
                                "ex captivity"
                            ],
                            "age": "20y",
                            "id": "343453509"
                        }
                    ]
                },
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "male"
                            ],
                            "age": "17y",
                            "id": "85604586"
                        }
                    ]
                },
                {
                    "name": "uganda",
                    "facts": [
                        {
                            "features": [
                                "young female"
                            ],
                            "age": "4y",
                            "id": "895673985"
                        },
                        {
                            "features": [
                                "none"
                            ],
                            "age": "11y",
                            "id": "39860394758936764"
                        }
                    ]
                }
            ]
        }
    ]
}

Например, следующий блок будет удален из JSON выше, поскольку id соответствует списку id_wanted

                    {
                        "features": [
                            "young female"
                        ],
                        "age": "4y",
                        "id": "2786438236"
                    }

Ответы [ 2 ]

0 голосов
/ 05 февраля 2019

Вы можете проанализировать original json и изменить полученную структуру данных на месте, используя удобный оператор *.:

def json = slurper.parseText(original)
json.animals*.countries*.facts*.each { facts ->
    facts.removeAll { fact -> fact.id in id_wanted }
}
def filtered = new JsonBuilder(json).toPrettyString()
println(filtered)

Вывод (с удаленными фактами из id_wanted):

{
    "animals": [
        {
            "name": "lion",
            "countries": [
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "young male"
                            ],
                            "age": "2y",
                            "id": "2837492"
                        }
                    ]
                },
                {
                    "name": "tanzania",
                    "facts": [

                    ]
                },
                {
                    "name": "south africa",
                    "facts": [
                        {
                            "features": [
                                "adult lioness"
                            ],
                            "age": "10y",
                            "id": "495684576"
                        }
                    ]
                }
            ]
        },
        {
            "name": "giraffe",
            "countries": [
                {
                    "name": "zambia",
                    "facts": [
                        {
                            "features": [
                                "ex captivity"
                            ],
                            "age": "20y",
                            "id": "343453509"
                        }
                    ]
                },
                {
                    "name": "kenya",
                    "facts": [
                        {
                            "features": [
                                "male"
                            ],
                            "age": "17y",
                            "id": "85604586"
                        }
                    ]
                },
                {
                    "name": "uganda",
                    "facts": [
                        {
                            "features": [
                                "none"
                            ],
                            "age": "11y",
                            "id": "39860394758936764"
                        }
                    ]
                }
            ]
        }
    ]
}
0 голосов
/ 05 февраля 2019

Предполагая, что ваш json является строкой в ​​переменной inputJson, вероятно, проще создать новый документ Json из оригинала с отфильтрованными значениями:

import groovy.json.*

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

List id_wanted = ['3894586', '2786438236', '895673985']

def result = new JsonBuilder([
    animals: json.animals.collect {[
        name: "$it.name",
        countries: it.countries.collect { [
            name: "$it.name",
            facts: it.facts.findAll { !(it.id in id_wanted) }
        ]}
    ]}
]).toString()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...