我在使用Jolt变换获得所需输出时遇到了麻烦.

我的输入JsonArray看起来像这样:

[
  {
    "from": [
      {
        "area1": 1
      },
      {
        "area2": 1
      },
      {
        "area3": 1
      }
    ],
    "id": 111,
    "to": "destination1"
  },
  {
    "from": [
      {
        "area1": 2
      },
      {
        "area2": 2
      },
      {
        "area3": 2
      }
    ],
    "id": 222,
    "to": "destination2"
  }
]

我正在使用Jolt Spec为JsonArray中的每个JsonObject创建一个JsonArray "from",并希望包含where "id"和"to"键:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "from": {
          "*": {
            "@": "[&]",
            "@(2,id)": "[&].id",
            "@(2,to)": "[&].to"
          }
        }
      }
    }
  }
]

这是输出:

[
  [
    {
      "area1": 1,
      "id": 111,
      "to": "destination1"
    },
    {
      "area1": 2
    }
  ],
  [
    {
      "area2": 1,
      "id": 111,
      "to": "destination1"
    },
    {
      "area2": 2
    }
  ],
  [
    {
      "area3": 1,
      "id": 111,
      "to": "destination1"
    },
    {
      "area3": 2
    }
  ]
]

我想要的输出应该是这样的:

[
  {
    "area1": 1,
    "id": 111,
    "to": "destination1"
  },
  {
    "area1": 2,
    "id": 222,
    "to": "destination2"
  },
  {
    "area2": 1,
    "id": 111,
    "to": "destination1"
  },
  {
    "area2": 2,
    "id": 222,
    "to": "destination2"
  },
  {
    "area3": 1,
    "id": 111,
    "to": "destination1"
  },
  {
    "area3": 2,
    "id": 222,
    "to": "destination2"
  }
]

我不知道为什么输入的第二个JSON Object中没有包含"id"和"to"键. 有没有人知道如何修复Jolt以获得所需的输出?

推荐答案

首先,不需要一个接一个地写外部属性

我建议以下shift作为第一个规范,如果在最外层数组中只有一个嵌套对象:

  {
    "operation": "shift",
    "spec": {
      "*": {
        "from": {
          "*": {
            "@2": { // replicate the respective values from 2 upper level
              "*": "[&1].&"
            },
            "*": "[&1].&" // replicate the attributes under objects those are under "from" arrays
          }
        }
      }
    }
  }

但是因为你有两次,所以需要将它们分开,这样做:在下面的完整转换中,分别用&4&3前缀标识符:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "from": {
          "*": {
            "@2": {
              "*": "&4[&1].&"
            },
            "*": "&3[&1].&"
          }
        }
      }
    }
  },
  { // get rid of the generated inner "from" arrays
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "from": ""
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

站点https://jolt-demo.appspot.com/上的demo是:

enter image description here

Json相关问答推荐

如何让jq输出长数据集?

我可以使用JQ来缩小数组中的json对象的范围吗?

创建Json嵌套文件 struct

无法使用Jolt变换在嵌套的JSON中提取值

Azure数据工厂-WEB活动输出赢得';t返回JSON

使用 jq 从带有转义反斜杠字符的嵌套 JSON 中提取数据

交换键和数组值,将旧键转换为新数组值,使用 jq

在 VS Code 中将一个正则表达式替换为另一个正则表达式

Jolt 转换数组对象并将某些字段移动到嵌套数组

将 JSON 字符串解析为 Kotlin Android 中的对象列表(MOSHI?)

从 PowerShell 编辑 Windows 终端配置文件设置 JSON

从 json 数组中仅提取一个值导致 vb6

如何在 Django 的模板语言中获取 json 键和值?

N1QL 聚合查询 Couchbase

如何让 JSON.NET 忽略对象关系?

如何从 HttpClient 解析 JSON 字符串?

C#扁平化json struct

如何在 Java 中将 YAML 转换为 JSON?

FastAPI:如何将正文读取为任何有效的 json?

JSON - 是否有任何 XML CDATA 类似功能?