Regex issue

Hello,

I was using Claude module to generate a response and I switched to Chatgpt.
I have the following output that I would like to parse in order to format it with JSON after:

Json
[
    {
        "Place": {
            "Name": "Les Catacombes de Paris",
            "Google_place_id": "ChIJVXealLU_xkcRja_At0z9AGY",
            "City": "Paris",
            "Region": "Île-de-France",
            "Country": "France",
            "Cultural_type": "",
            "Facts": {
                "Historical_facts": {
                    "FACT_TYPE": "Did you know ?",
                    "FACTS": [
                        "Les Catacombes de Paris ont été créées à la fin du 18ème siècle pour transférer les restes humains de cimetières surpeuplés de Paris."
                    ],
                    "INTERACTION": [
                        "Peux-tu imaginer comment ils ont transporté tous ces ossements en sous-sol ?"
                    ],
                    "CATEGORY": "Historical_facts"
                },
                "Architectural_facts": {
                    "FACT_TYPE": "Wow",
                    "FACTS": [
                        "Les Catacombes s'étendent sur plus de 300 kilomètres sous les rues de Paris, même si seulement une petite partie est ouverte au public."
                    ],
                    "INTERACTION": [
                        "Combien de fois pourrais-tu aller à l'école avec cette distance sous tes pieds ?"
                    ],
                    "CATEGORY": "Architectural_facts"
                },
                "Scientific_facts": {
                    "FACT_TYPE": "Fun fact",
                    "FACTS": [
                        "Les Catacombes sont un endroit fascinant pour les chercheurs qui étudient la chimie des ossements pour apprendre davantage sur la vie des Parisiens d'autrefois."
                    ],
                    "INTERACTION": [
                        "Que penses-tu qu'on puisse apprendre en étudiant de vieux os ?"
                    ],
                    "CATEGORY": "Scientific_facts"
                },
                "Mystery_legends_facts": {
                    "FACT_TYPE": "Did you know ?",
                    "FACTS": [
                        "Une légende urbaine raconte que la nuit, on peut entendre des voix mystérieuses dans les Catacombes. C'est pourquoi certains disent qu'elles seraient hantées !"
                    ],
                    "INTERACTION": [
                        "Aimerais-tu explorer un endroit aussi mystérieux, accompagné bien sûr ?"
                    ],
                    "CATEGORY": "Mystery_legends_facts"
                }
            }
        }
    }
]

Here is the Regex I use: (?<json>{[\w\W]*?"Place": \{(?:.*\n)+?})
it was working with Claude output but doesn’t work with Chatgpt, I can’t see where is the error coming from?
Many thanks

Hey @NicoM
What about this regex?

Json(?<json_output>[\s\S]*)

Then could use a JSON Parse module to parse the field called “json_output”.

when parsing the output, you get a JSON array ready to be processed

Hi @PierreL

thank you for your response, unfortunately, not more luck.
It s not validated by Regex101: https://regex101.com/r/i5fqQx/2

yes because your output has changed again - see screenshot

use this one then:

```json(?<json_output>[\s\S]*)```

If I were you I would strengthen my OpenAI prompt when asking for a JSON output.
Using sentences such as:

  • your output must be a STRICT JSON file
  • always start your output with an opening curly bracket
  • always end your output with an closing curly bracket

Ah I see,
That’s why I was initially using this regex: (?<json>{[\w\W]*?"Place": \{(?:.*\n)+?})
As it was ignoring everything before “Place” so if chatgpt write Json or json at the beginning or anything else it was ignore.
I have strenghen the prompt (style have this ````json before though) and replaced the regex from Json(?<json_output>[\s\S]*) to json(?<json_output>[\s\S]*)
It is working but i m not sure it will last

Last output was:

```json
{
    "Place": {
        "Name": "Les Catacombes de Paris",
        "Google_place_id": "ChIJVXealLU_xkcRja_At0z9AGY",
        "City": "Paris",
        "Region": "Île-de-France",
        "Country": "France",
        "Cultural_type": "Historical",
        "Facts": {

I’ve tweaked the regex: (?<json_output>\{[\s\S]*\})
It seems to work properly
Thanks for you help!

3 Likes

Welcome to the Make community!

Please see these duplicate questions previously asked and answered by other community members:

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.