我正在尝试将 JSON 转换为以下预期输出。坚持下面的规范。有人可以帮忙吗?
“结果”数组中有一个名为“内容”的内部数组,我想将其作为主数组的一部分。
输入 JSON
{
"total": 100,
"start": 1,
"page-length": 10,
"results": [
{
"index": 1,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "A1",
"region": "APAC"
}
]
}
},
{
"index": 2,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "B1",
"region": "AMER"
}
]
}
},
{
"index": 3,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "C1",
"region": "APAC"
}
]
}
}
]
}
预期的 json 输出
[
{
"code": "A1",
"region": "APAC"
},
{
"code": "B1",
"region": "AMER"
},
{
"code": "C1",
"region": "APAC"
}
]
规范尝试
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"extracted": { "content": { "@": "&" } }
}
}
}
},
{
"operation": "shift",
"spec": {
"content": {
"@": "&"
}
}
}
]
最佳答案
您可以使用嵌套在方括号内的#通配符来达到“results”
数组的索引级别,这样
[
{
"operation": "shift",
"spec": {
"results": {
"*": {//the indexes of the "results" array
"extracted": {
"content": {
"*": {//the indexes of the "content" array
"*": "[#5].&"
}
}
}
}
}
}
}
]
此外,以下规范在没有键的情况下重复内部数组的内容,将给出相同的结果:
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"extracted": {
"content": {
"*": "[]"// [] seems like redundant but kept for the case the array has a single object.
}
}
}
}
}
}
]
这与您尝试过的非常相似。
关于arrays - 震动 : Make Nested Make a nested array as part of main array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75311456/