Я мог бы использовать некоторую помощь в написании скрипта Groovy, чтобы сгладить и разделить JSON на несколько JSON на основе вложенных элементов массива. Вот исходный JSON:
{
"input_query": {
"discount_guid": "3afeb169-7969-4f6f-8928-d801692848b1",
"user_uid": 5467890,
"shopping_list": [
{
"article_id": 311729,
"current_price_without_promo": 7.69,
"promo_discount": 0,
"count": 1,
"apply_discount": true
},
{
"article_id": 229752,
"current_price_without_promo": 11.29,
"promo_discount": 0,
"count": 1,
"apply_discount": true
},
{
"article_id": 193672,
"current_price_without_promo": 79.99,
"promo_discount": 0,
"count": 1,
"apply_discount": true
},
{
"article_id": 261657,
"current_price_without_promo": 16.99,
"promo_discount": 0,
"count": 1,
"apply_discount": true
},
{
"article_id": 318153,
"current_price_without_promo": 13.99,
"promo_discount": 0,
"count": 1,
"apply_discount": true
}
],
"discount_params_per_article": [
{
"article_id": 311729,
"min_discount": 0,
"max_discount": 4.12,
"imposed_discount": null,
"article_target_probability_increase": 1.15,
"discount_downscale_factor": 1
},
{
"article_id": 229752,
"min_discount": 0,
"max_discount": 7.52,
"imposed_discount": null,
"article_target_probability_increase": 1.15,
"discount_downscale_factor": 1
},
{
"article_id": 193672,
"min_discount": 0,
"max_discount": 60,
"imposed_discount": null,
"article_target_probability_increase": 1.15,
"discount_downscale_factor": 1
},
{
"article_id": 261657,
"min_discount": 0,
"max_discount": 12.4,
"imposed_discount": null,
"article_target_probability_increase": 1.15,
"discount_downscale_factor": 1
},
{
"article_id": 318153,
"min_discount": 0,
"max_discount": 8,
"imposed_discount": null,
"article_target_probability_increase": 1.15,
"discount_downscale_factor": 1
}
],
"target_probability_increase": null,
"request_time": "2019-12-21T21:32:13.018635"
},
"total_discount": 0.94,
"article_discounts": [
{
"article_id": 311729,
"discount": 0.04
},
{
"article_id": 229752,
"discount": 0.08
},
{
"article_id": 193672,
"discount": 0.61
},
{
"article_id": 261657,
"discount": 0.13
},
{
"article_id": 318153,
"discount": 0.08
}
]
}
Я бы хотел сгладить исходный JSON с массивом JSON, например:
[{
"discount_guid": "3afeb169-7969-4f6f-8928-d801692848b1",
"user_uid": 5467890,
"article_id": 318153,
"current_price_without_promo": 13.99,
"promo_discount": 0,
"count": 1,
"apply_discount": true,
"min_discount": 0,
"max_discount": 8,
"imposed_discount": null,
"article_target_probability_increase": 1.15,
"discount_downscale_factor": 1,
"target_probability_increase": null,
"request_time": "2019-12-21T21:32:13.018635",
"total_discount": 0.94,
"discount": 0.08
},
{
"discount_guid": ...
},
...
]
Мне удалосьполучить один сплющенный JSON следующим образом:
import groovy.json.JsonOutput as jo
def content = new File('response.json')
def slurper = new groovy.json.JsonSlurper()
def object = slurper.parseText(content)
def flattenMap(Map map) {
def result = [:]
map.each { k, v ->
if (v instanceof Map) {
result << flattenMap(v)
} else if (v instanceof Collection && v.every {it instanceof Map}) {
v.each {
result << flattenMap(it)
}
} else {
result[k] = v
}
}
result
}
println(jo.prettyPrint(jo.toJson(flattenMap(object))))
Но я понятия не имею, как получить полный массив JSON-ов. Я уверен, что есть простой способ сделать это, но я новичок в Groovy, и до сих пор не нашел решения. Любая помощь будет принята с благодарностью.