如何将子级合并到jsonata中的数组

问题描述 投票:0回答:1

如何合并“123”和“Trilby hat”中的对象

{
"123": {
"Price": 68.9,
"Qty": 2,
"ProductID": 858383
},
"Trilby hat": {
"Price": 21.67,
"Qty": 1,
"ProductID": 858236
}
}

{
"Items" : \[
{
"Price": 68.9,
"Qty": 2,
"ProductID": 858383
},  
{
"Price": 21.67,
"Qty": 1,
"ProductID": 858236
}
\]
}

这是示例https://try.jsonata.org/Q84as3J_3

jsonata
1个回答
0
投票

只需获取条目值,并将它们分配给

Items

const actual = { Items: Object.values(raw) };

演示

const raw = {
  "123": {
    "Price": 68.9,
    "Qty": 2,
    "ProductID": 858383
  },
  "Trilby hat": {
    "Price": 21.67,
    "Qty": 1,
    "ProductID": 858236
  }
};


const expected = {
  "Items": [{
    "Price": 68.9,
    "Qty": 2,
    "ProductID": 858383
  }, {
    "Price": 21.67,
    "Qty": 1,
    "ProductID": 858236
  }]
};

const actual = { Items: Object.values(raw) };

console.log(JSON.stringify(actual) === JSON.stringify(expected));

© www.soinside.com 2019 - 2024. All rights reserved.