Replace comma with counting number

Hi all,

I’ve built a scenario that conducts research using the Perplexity module. The module outputs an array called “citations”, which contains the sources. Within the research text (specifically in the “message - content” field), these sources are referenced using [x+1], starting from [1] and increasing sequentially for each new citation. However, the total number of sources is unknown beforehand.

Currently, when I output the “citations” array, all sources appear in a single line, separated only by commas. Instead, I’d like to format them so that each source is listed with its corresponding reference number, like this:

[1] Source 1
[2] Source 2
[x+1] Source x

How can I transform the array into this structured format?

I’d really appreciate your help, thanks in advance!

Hi @Thomas-SopraSteria

The split function can convert a comma-separated text into an array.

1 Like

Hi Ponvaskon, thanks for your reply! However, I’m not sure how this would help me, as the citations are already in an array. Additionally, I’m unsure how to proceed with converting the array into a list using the [x+1] format. Could you please clarify?

Hi @Thomas-SopraSteria

Please provide an example of a JSON “citations” array and the required data structure you want.

This is the citations array from the Perplexity output:

"citations": [
            "https://www.soprasteria.com/about-us/our-offices",
            "https://careers.soprasteria.com/who-are-we",
            "https://www.soprasteria.co.uk/about-us/our-business",
            "https://dcfmodeling.com/blogs/vision/soppa-mission-vision",
            ...
        ]

When importing this into another module it looks like this:

 "content": "https://www.soprasteria.com/about-us/our-offices, https://careers.soprasteria.com/who-are-we, https://www.soprasteria.co.uk/about-us/our-business, https://dcfmodeling.com/blogs/vision/soppa-mission-vision, ..."

I want it to become this data structure:

"citations": [
            "[1] https://www.soprasteria.com/about-us/our-offices
            [2] https://careers.soprasteria.com/who-are-we
            [3] https://www.soprasteria.co.uk/about-us/our-business
            [4] https://dcfmodeling.com/blogs/vision/soppa-mission-vision
            [x+1] ... "
        ]

Again, thanks for your willingness to help me!

Hi @Thomas-SopraSteria

  1. You should parse the Perplexity output using the Parse JSON module. You will get the array with the citations.

  2. Iterate this array and aggregate it into a text. Specify the bundle order position variable in the text aggregator as on the screenshot.


  3. Transform the text into JSON if needed.

2 Likes

Thank you for this detailed guide, it works perfectly! I actually only needed step 2 for my use case.

2 Likes