英文:
How to remove the square brackets from the Array Variable
问题
我有一个数组,是通过for each
循环得到的,如下所示。
如何移除方括号,因为我需要将没有方括号的输出分配给不同的JSON负载。
我尝试使用以下代码:
replace(replace(string(outputs('Compose')),'[',''),'','')```
但是它会删除方括号,但再次将数据转换为字符串,如下所示:
"{"cont":"","productIdSAT":"53131602","qunt":"1","srvcId":"PACKETS","volH":"22","volL":"17.5","volW":"27","weight":"1"},{"cont":"","productIdSAT":"53131602","qunt":"1","srvcId":"PACKETS","volH":"22","volL":"17.5","volW":"27","weight":"1"},{"cont":"","productIdSAT":"53131602","qunt":"1","srvcId":"PACKETS","volH":"22","volL":"17.5","volW":"27","weight":"1"}"
我还尝试将其转换为JSON,但在使用表达式时,它只进行了一次转换,而不是3个项,输出如下:
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
但是期望的输出是:
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
}
请告诉我如何实现这一目标。
<details>
<summary>英文:</summary>
I have an array coming as a result of for each loop as shown below.
[
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
}
]
How to remove the square brackets as I need to assign the output without square brackets to different json payload.
I have tried using **replace(replace(string(outputs('Compose')),'[',''),']','')**
But it removes the brackets but again converts the data into string like this
"{"cont":"","productIdSAT":"53131602","qunt":"1","srvcId":"PACKETS","volH":"22","volL":"17.5","volW":"27","weight":"1"},{"cont":"","productIdSAT":"53131602","qunt":"1","srvcId":"PACKETS","volH":"22","volL":"17.5","volW":"27","weight":"1"},{"cont":"","productIdSAT":"53131602","qunt":"1","srvcId":"PACKETS","volH":"22","volL":"17.5","volW":"27","weight":"1"}"
I have tried to convert it to Json but when i use the expression it is only converting and instead of 3 items it is giving ouput as
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
but output expected
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
}
Please let me know how this can be achieved.
</details>
# 答案1
**得分**: 1
JSON标准不允许你拥有需要的输出对象。但是,如果你打算将结果作为字符串,我的建议是:
```javascript
let data = [
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
}
];
let text = JSON.stringify(data);
let output = text.replace(/\[|\]/g, '');
如果你打算使用JavaScript,这可能是一种方法。
我的测试
但最终,如果你想将结果(output)用作JSON对象,我认为这并不有用。
希望这有点帮助。
英文:
The JSON standard does not let you have an object with that output you need. But, if you are thinking to have the result as an string, my suggestion is
let data = [
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
},
{
"cont": "",
"productIdSAT": "53131602",
"qunt": "1",
"srvcId": "PACKETS",
"volH": "22",
"volL": "17.5",
"volW": "27",
"weight": "1"
}
];
let text = JSON.stringify(data);
let output = text.replace(/\[|\]/g,'');
if you are going to use javascript, that could be the way.
My test
But at the end, if you want to use the result (output) as a JSON object I think this is not useful.
Hope this help a little bit
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论