javascript - JSONata 获取父元素

标签 javascript traversal jsonata

在一个相当复杂的 JSON 对象中,我试图获取带有父值的键。

{
    "provinces": [
        {
            "short": "ska",
            "tiletype": "water",
            "provinceOutlinePath": "M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z",
            "unionParts": [
                {
                    "id": "main",
                    "unionPartPath": "M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z"
                },
 {
                    "id": "main",
                    "unionPartPath": "M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z"
                }
            ]
        },
        {
            "short": "nws",
            "tiletype": "water",
            "provinceOutlinePath": "M140.038,0.667l34.86,68.197l-6.338,27.888l12.677,3.169l70.988,-17.747l100.144,-62.115l1.268,-19.392l-213.599,0Z",
            "unionParts": [
                {
                    "id": "main",
                    "unionPartPath": "M140.038,0.667l34.86,68.197l-6.338,27.888l12.677,3.169l70.988,-17.747l100.144,-62.115l1.268,-19.392l-213.599,0Z"
                }
            ]
        }
   ]
}

我想将对象更改为:

[
  { 
    "short": ska,
    "unionPartPath": "<Path>"
  },
  { 
    "short": ska,
    "unionPartPath": "<AnotherPath>"
  },
  { 
    "short": nws,
    "unionPartPath": "<Path>"
  }
]

我已经浏览了整个文档,但没有发现任何类似于 .parent() 方法的内容。
也许可以通过一些高阶函数来实现所需的结果,但目前我不知道如何实现这一点。

最佳答案

要在 JSONata 中执行此操作,您需要以下表达式

provinces.($short := short; unionParts.{
  'short': $short,
  'unionPartPath': unionPartPath
})

参见http://try.jsonata.org/H1goy6AjE

关于javascript - JSONata 获取父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55976740/

相关文章:

json - 使用 Jsonata 匹配名称包含空格或特殊字符的 json 字段

jsonata - 如果数组包含特定字符串则返回 True - JSONata

javascript - 如何使用express显示实时变量node.js

javascript - 使用 whatsapp 网络共享数据

C - 二维数组向外(从中心开始)顺时针螺旋遍历

traversal - 将 Gremlin 与 Java API 用于 OrientDB

javascript - 如何将嵌套对象数组转换为 CSV?

jsonata - 如何从子节点访问父节点

javascript - Angular 4 ng-content select 不适用于动态创建的嵌入元素

javascript - iPhone 的登录表单