Skip to main content

Notifications

Community site session details

Community site session details

Session Id : QvTA9fklfKRZauXmf4hmqd
Power Automate - General Discussion
Unanswered

Need a Function to remove [ ] Square brackets from an array

Like (0) ShareShare
ReportReport
Posted on 14 May 2023 03:23:27 by 4

Hello everyone,

I am encountering some issues while attempting to remove the square brackets [ ] from an ARRAY. Here is an example of the scenario:

 

Let's call this array "ContextArray".

 

I am using CHAT-GPT to generate answers based on previous interactions, and the "ContextArray" is placed within an HTTP request. This HTTP request is within a "Do While Loop" because the "ContextArray" will be fed with new responses from chat GPT, and the array will increase from (see image 1) to (see image 2) to (see image 3). However, the HTTP request will only receive the array without the [ ] brackets; otherwise, it will give me an error in the request.

 

I cannot use the array index since I need to display all the objects at once.

 

Is there a way to display all the items within the array? For example, instead of "variables('Array')[0]" or "variables('Array')[1]", something like "variables('Array')[ALL]".

 

I have already attempted to convert the array to a string, but it did not work.

 

Any ideas regarding this matter would be much appreciated. Thank you.

 

Image_1

Danatt_0-1684033082214.png

Image_2

Danatt_1-1684034304565.png

Image_3

Danatt_2-1684034333117.png

 

 

Categories:
  • Chriddle Profile Picture
    7,685 Super User 2025 Season 1 on 19 Jan 2024 at 13:07:05
    Re: Need a Function to remove [ ] Square brackets from an array

    However, if the objects already are in the preferred shape, why not simply join them?

    Chriddle_0-1705669579235.png

     

     

    join(outputs('Compose'), ',')

     

     

     

    Result:

     

     

    {"role":"assistant","content":"R1"},{"role":"assistant","content":"R1"}

     

     

    But it seems that is not your problem anymore 🤣

  • Chriddle Profile Picture
    7,685 Super User 2025 Season 1 on 19 Jan 2024 at 12:58:26
    Re: Need a Function to remove [ ] Square brackets from an array

    In most cases, it's not a good idea to perform string manipulations on a JSON (unless you know exactly what you're doing 😉).

     

    Depending on how you want to display the objects, I have two proposals:

    • "Create HTML table"
    • Put the array into a Select, map the objects to strings in the way you want them and in the next action join() these strings.
  • NER Profile Picture
    24 on 19 Jan 2024 at 12:22:59
    Re: Need a Function to remove [ ] Square brackets from an array

    Thank you for your answer, it works for me. However, if you wish to replace "," is another problem that belongs to the array itself and if your string has any comma for example 15,30. It replaces the string itself also and shows 1530.

  • SriniK Profile Picture
    3,025 Super User 2025 Season 1 on 15 May 2023 at 14:34:55
    Re: Need a Function to remove [ ] Square brackets from an array

    @Danatt 

     

    I'm checking with you, Is the Image1, Image2, Image3 are from the same request?

    Can you paste the response in a notepad file and attach here?

     

    Thanks,

    Srini

  • Nived_Nambiar Profile Picture
    17,534 Super User 2025 Season 1 on 15 May 2023 at 01:46:45
    Re: Need a Function to remove [ ] Square brackets from an array

    Hi @Danatt 

     

    I saw in openAI Api documentation, messages are passed with square brackets in request body 

     

    Nived_Nambiar_0-1684115163161.png

     

     

    Nived_Nambiar_1-1684115184442.png

     

     

    Hope it helps !

     

    Mark it as solution if it resolves your query !

     

  • Danatt Profile Picture
    4 on 14 May 2023 at 14:55:05
    Re: Need a Function to remove [ ] Square brackets from an array

    Dear @Nived_Nambiar,

     

    Thank you for your response.

     

    I have tried using the function that you suggested, but the Compose output I received is not as expected. While the square brackets were successfully removed, the curly brackets "{ }" are now accompanied by colons and backslashes. 

     

    The desired output should be in the following format:

     

    {"role":"assistant","content":"Respuesta GPT 0"},{"role":"user","content":"Respuesta Votacion 0"}

     

    However, when I incorporate the output of the Compose action in the HTTP request, the result appears as follows:

     

    "{\"role\":\"assistant\",\"content\":\"Respuesta GPT 0\"},{\"role\":\"user\",\"content\":\"Respuesta Votacion 0\"}"
     

    I have attempted to remove the backslashes, as well as the initial and final colon using a replace function, but it appears that these symbols are implicit within the string.

     

    Finally this is the error of the HTTP:

     

    {
      "error": {
        "message": "'{\"role\":\"assistant\",\"content\":\"Test Assistant 0\"},{\"role\":\"user\",\"content\":\"Test Votacion 0\"}' is not of type 'object' - 'messages.2'",
        "type": "invalid_request_error",
        "param": null,
        "code": null
      }
    }
     

    Thank you.

  • Nived_Nambiar Profile Picture
    17,534 Super User 2025 Season 1 on 14 May 2023 at 07:57:56
    Re: Need a Function to remove [ ] Square brackets from an array

    Hi @Danatt 

     

    Give a try like this 

     

    replace(replace(string(variables('Array')),'[',''),']','')

     

    Hope it helps 🙂

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,745 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,091 Most Valuable Professional

Leaderboard
Loading started