我try 使用Jolt将数组中每个值的一个键映射到一个新array. 有没有人能帮我解决这个问题:

我的JSON:

[
  {
    "person_id": "1",
    "resources": ["asd", "zxc"]
  },
  {
    "person_id": "2",
    "resources": ["ghj", "asd"]
  }
]

和我期望的JSON:

[
  {
    "person_id": "1",
    "resource": "asd"
  },
  {
    "person_id": "1",
    "resource": "zxc"
  },
  {
    "person_id": "2",
    "resource": "ghj"
  },
  {
    "person_id": "2",
    "resource": "asd"
  }
]

我试过这个Jolt规格

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "resources": {
          "*": {
            "@(2,person_id)": "[&].person_id",
            "@": "[&].resource"
          }
        }
      }
    }
  }
]

但不幸的是,它总是将同一索引中的所有值映射到一个array.

推荐答案

通过遍历第一个转换规范中的100数组,可以使用两个连续的shift转换规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*s": { // this tecnique stands for extracting by the replacement of the asterisk through use of &(2,1) below
          "*": {
            "@(2,person_id)": "[&3].&1.person_id", // to separate by indexes of the array to generate three level for three independent objects
            "@": "[&3].&1.&(2,1)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

站点http://jolt-demo.appspot.com/上的101

enter image description here

Json相关问答推荐

JSON字符串处理注入引号

我发现GoFr响应总是包含在数据字段中,如何返回自定义响应?

如果主对象中已存在属性,则不应在Jolt中引用次对象

如何在VB6中将字符串转换或解码为可读格式?

展平多个数组以保持顺序

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

如何按键过滤

如何使用 jq 将字符串数组转换为对象?

数据清理设计不良的 JSON 数据 - 需要有关最佳策略的建议

如何加入或合并列表元素列表(未知长度)

JSONPath:查找子项目条件在字符串列表中的项目

如何迭代、动态加载我的表单输入元素,然后在 React 中的表单提交上检索输入值?

谷歌浏览器不允许我放置断点

apple-app-site-association json 文件是否会在应用程序中更新?

使用 jq 从 bash 中管道分隔的键和值创建 JSON

在自定义 JsonConverter 的 ReadJson 方法中处理空对象

如何按键查找特定的 JSON 值?

如何让 javascript 从 .json 文件中读取?

将 json 转换为 C# 数组?

如何使用 SwiftyJSON 遍历 JSON?