2

在一个相当复杂的 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()方法的东西。
也许可以通过一些更高阶的函数来实现预期的结果,但目前我不知道如何实现这一点。

4

4 回答 4

2

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

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

http://try.jsonata.org/H1goy6AjE

于 2019-05-07T08:21:36.597 回答
1

一个简单的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);

于 2019-05-03T20:54:30.730 回答
1

你可以尝试这样的事情。

在这里,我使用了在数组上定义的map()方法。请确保使用正确的路径。

<yourPath>正如你所提到的,我刚刚指定了<Path><AnotherPath>(你很清楚这是什么意思,所以只需替换)

将对象分配给任何变量,例如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
> 
于 2019-05-03T20:50:06.610 回答
0

这是一种将Array.reduce()解构功能结合使用的解决方案:

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;}

于 2019-05-03T21:06:57.163 回答