如何从数组变量中移除方括号

huangapple go评论120阅读模式
英文:

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(&#39;Compose&#39;)),&#39;[&#39;,&#39;&#39;),&#39;]&#39;,&#39;&#39;)**
But it removes the brackets but again converts the data into string like this

"{&quot;cont&quot;:&quot;&quot;,&quot;productIdSAT&quot;:&quot;53131602&quot;,&quot;qunt&quot;:&quot;1&quot;,&quot;srvcId&quot;:&quot;PACKETS&quot;,&quot;volH&quot;:&quot;22&quot;,&quot;volL&quot;:&quot;17.5&quot;,&quot;volW&quot;:&quot;27&quot;,&quot;weight&quot;:&quot;1&quot;},{&quot;cont&quot;:&quot;&quot;,&quot;productIdSAT&quot;:&quot;53131602&quot;,&quot;qunt&quot;:&quot;1&quot;,&quot;srvcId&quot;:&quot;PACKETS&quot;,&quot;volH&quot;:&quot;22&quot;,&quot;volL&quot;:&quot;17.5&quot;,&quot;volW&quot;:&quot;27&quot;,&quot;weight&quot;:&quot;1&quot;},{&quot;cont&quot;:&quot;&quot;,&quot;productIdSAT&quot;:&quot;53131602&quot;,&quot;qunt&quot;:&quot;1&quot;,&quot;srvcId&quot;:&quot;PACKETS&quot;,&quot;volH&quot;:&quot;22&quot;,&quot;volL&quot;:&quot;17.5&quot;,&quot;volW&quot;:&quot;27&quot;,&quot;weight&quot;:&quot;1&quot;}"




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 = [
    {
        &quot;cont&quot;: &quot;&quot;,
        &quot;productIdSAT&quot;: &quot;53131602&quot;,
        &quot;qunt&quot;: &quot;1&quot;,
        &quot;srvcId&quot;: &quot;PACKETS&quot;,
        &quot;volH&quot;: &quot;22&quot;,
        &quot;volL&quot;: &quot;17.5&quot;,
        &quot;volW&quot;: &quot;27&quot;,
        &quot;weight&quot;: &quot;1&quot;
    },
    {
        &quot;cont&quot;: &quot;&quot;,
        &quot;productIdSAT&quot;: &quot;53131602&quot;,
        &quot;qunt&quot;: &quot;1&quot;,
        &quot;srvcId&quot;: &quot;PACKETS&quot;,
        &quot;volH&quot;: &quot;22&quot;,
        &quot;volL&quot;: &quot;17.5&quot;,
        &quot;volW&quot;: &quot;27&quot;,
        &quot;weight&quot;: &quot;1&quot;
    },
    {
        &quot;cont&quot;: &quot;&quot;,
        &quot;productIdSAT&quot;: &quot;53131602&quot;,
        &quot;qunt&quot;: &quot;1&quot;,
        &quot;srvcId&quot;: &quot;PACKETS&quot;,
        &quot;volH&quot;: &quot;22&quot;,
        &quot;volL&quot;: &quot;17.5&quot;,
        &quot;volW&quot;: &quot;27&quot;,
        &quot;weight&quot;: &quot;1&quot;
    }
];

let text = JSON.stringify(data);
let output = text.replace(/\[|\]/g,&#39;&#39;);

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

huangapple
  • 本文由 发表于 2023年3月10日 01:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定