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
Image_2
Image_3
However, if the objects already are in the preferred shape, why not simply join them?
join(outputs('Compose'), ',')
Result:
{"role":"assistant","content":"R1"},{"role":"assistant","content":"R1"}
But it seems that is not your problem anymore 🤣
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:
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.
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
Hi @Danatt
I saw in openAI Api documentation, messages are passed with square brackets in request body
Hope it helps !
Mark it as solution if it resolves your query !
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:
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:
Thank you.
Hi @Danatt
Give a try like this
replace(replace(string(variables('Array')),'[',''),']','')
Hope it helps 🙂
WarrenBelz
146,745
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
66,091
Most Valuable Professional