arrays - 震动 : Make Nested Make a nested array as part of main array

标签 arrays json transformation jolt

我正在尝试将 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": {
        "@": "&"
      }
    }
  }

]

找到下面我在 Jolt 工具上得到的输出 enter image description here

最佳答案

您可以使用嵌套在方括号内的#通配符来达到“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/

相关文章:

arrays - 赋值左侧的函数调用必须返回 Variant 或 Object,在两个数组中查找公共(public)值

java - 如何初始化数组以便稍后在测试方法中使用 (Java)

ios - 当我将 NSString 转换为 JSON 时,我得到 null

xml - XSLT:将 namespace 设置为第一个属性

r - R 中的 log10 转换

Azure Blob 到 Azure SQL 数据库 - 数据工厂

c++ - 指向二维数组的指针 C、C++

arrays - Postgres : why does this GIN index not used for this "object in array" query

javarest返回json响应给 postman - 语法错误: unexpected 'S'

python - 如何获取多个JSON对象的值?