JSONata 获取父元素

JSONata get parent element

在一个相当复杂的 JSON-object 中,我试图获得一个具有父值的键。

{
    "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() 方法的东西。
也许可以通过一些高阶函数来实现预期的结果,但目前我不知道如何实现它。

你可以试试这个。

Here I have used map() method defined on Arrays. You please make sure to use proper path.

I have just specified <yourPath> as you have mentioned <Path>, <AnotherPath> (You know well that what does it means so just replace)

将对象分配给任何变量,例如到 obj。现在我们可以使用这 1 行语句来获取结果

result = obj.provinces.map((obj2) => obj2.unionParts.map((obj3) => {return {"short": obj2.short, "unionPartPath": "<yourPath>"}}))

初始化 »

> let obj = {
...     "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"
.......                 }
.....             ]
.....         }
...    ]
... }
undefined
> 

终于 »

> result = obj.provinces.map((obj2) => obj2.unionParts.map((obj3) => {return {"short": obj2.short, "unionPartPath": "<yourPath>"}}))
[ [ { short: 'ska', unionPartPath: '<yourPath>' },
    { short: 'ska', unionPartPath: '<yourPath>' } ],
  [ { short: 'nws', unionPartPath: '<yourPath>' } ] ]
> 

正在漂亮地打印对象 »

> console.log(JSON.stringify(result, null, 4)) // Pretty print of object
[
    [
        {
            "short": "ska",
            "unionPartPath": "<yourPath>"
        },
        {
            "short": "ska",
            "unionPartPath": "<yourPath>"
        }
    ],
    [
        {
            "short": "nws",
            "unionPartPath": "<yourPath>"
        }
    ]
]
undefined
> 

一个简单的 for-of 循环就可以做到:

const jsonData = {
  "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"
      }]
    }
  ]
};

const result = [];
for (let p of jsonData.provinces) {
  for (let part of p.unionParts) {
    result.push({short: p.short, unionPartPath: part.unionPartPath});
  }
}

console.log(result);

这是一种使用 Array.reduce() in conjunction with destructuring 功能的解决方案:

const jsonData = {"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"}]}]};


let res = jsonData.provinces.reduce((acc, {short, unionParts}) =>
{
    unionParts.forEach(({unionPartPath}) => acc.push({short, unionPartPath}));
    return acc;
}, []);


console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

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

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

http://try.jsonata.org/H1goy6AjE