Stuck with REGEX to extract JSON object

Hey guys, I am getting some messages from ChatGPT that might be a JSON, or they may contain a JSON.

I want to create a Text Parser module that only extracts the JSON, so I can then use it in subsequent modules.

I have built the Regex formula, if I test it on the website provided in the documentation of Make.com, the Regex formula works perfectly well. - www.regex101.com

Sample Text 1

Sample Text 2

However, if I try to run the same Regex formula, I am getting the following result:

Here is the module setup:

Here are the sample texts:

Text 1:

{
“link”: “https://actexpo2024.smallworldlabs.com/exhibitors”,
“Request Type”: “GET”,
“Request Body”: “”
}

Text 2:

Here is the JSON structure compiled based on your answer:

{
“link”: “https://actexpo2024.smallworldlabs.com/exhibitors”,
“Request Type”: “GET”,
“Request Body”: “”
}
This structure indicates that the request made to the URL is of type GET and there is no request body involved, which is typical for GET requests. If you need to adjust or add additional details, let me know!


Any idea why I am getting an empty collection as a result from the Text Parser module?

PS: I have no idea how to build regex formulas. I am building them with ChatGPT and testing them on regex101.com. So I am not sure why the formula is set up the way that it is.

Can you post a full screenshot of your configuration of the Text Parser module (including the advanced options)? It may have something to do with the configuration you have the Text Parser module in.

1 Like

Hey Khurram,

Thanks for the prompt response!

Here:

Welcome to the Make community!

Screenshot_2024-02-20_151445

You can use a Text Parser “Match Pattern” module with this Pattern (regular expression):

{[\w\W]+?}

Proof

https://regex101.com/r/QzGZKm

Important Info

  • :warning: Global match must be set to NO!

For more information, see Text Parser in the Make Help Center:

Match Pattern
The Match pattern module enables you to find and extract string elements matching a search pattern from a given text. The search pattern is a regular expression (aka regex or regexp), which is a sequence of characters in which each character is either a metacharacter, having a special meaning, or a regular character that has a literal meaning.

Hope this helps!

samliew – request private consultation

2 Likes

In Regex101 you have /gm enabled while in Make you have Global match and Multiline disabled. Can you please enable those two and see if that makes a difference?

1 Like

Hey Sam, This is exactly what I am using as a module.

I have tried your formula:

Here is the result:

Still the same problem

Hey Khurram,

I have tried enabling the Global match and it works:

However, this formula seems to NOT work when there is more text surrounding the JSON. It doesn’t matter if I also add multiline or not.

At the same time it works perfectly well on regex101.com

Here is the regex formula

^\s*\{[\s\S]*?\}\s*$

Here is the example text:

Here is the JSON structure compiled based on your answer:  

{   
"link": "https://actexpo2024.smallworldlabs.com/exhibitors",   
"Request Type": "GET",   
"Request Body": "" 
} 

This structure indicates that the request made to the URL is of type GET and there is no request body involved, which is typical for GET requests. If you need to adjust or add additional details, let me know!

Hi Alexander,

The regex works when grouped, ({[\w\W]+?}) but if you are going to do that and there is only going to be one match then a named group (?<json>{[\w\W]+?}) is probably a cleaner option. I have attached a recording showing the behavior.

You may already be doing it, but just incase you aren’t you can also prompt ChatGPT for specific output, in the recording I have added a second prompt to define how I want my response, the regex will still be the same in such scenarios but ChatGPT can produce structured data only if prompted so, but you may want to set out instructions for when it can’t fulfil a request too.

3 Likes

Hey Scott, this actually works perfectly well!

With this formula, I don’t have to use global matching either. I have run the formula for some analysis in ChatGPT, and it works amazingly.

I didn’t know you could name different groups. This seems to be very handy, and I will be using it in the future.


To your ChatGPT comment - I am actually trying to build an assistant. I realized that building an assistant and using functions with make is not very easy, as you have to be pinging the assistant constantly, and you cannot make it trigger a webhook.

Hence, I am using the assistant within the Playground, but I want it to give me some structured data, that I can easily pass to make.com, make a few HTTP requests, and pass a file to my assistant that it can then use for analysis.

It’s a bit complex, but as I don’t know how to use the Assistant functions, without constantly pinging open.ai and also assistants, trigger a response to a message request if you confirm that you want to use a particular function, which breaks make.com. I don’t know how to get around those 2 limitations, but I still want to use assistants even semi-automatically

Otherwise, whenever I request structured data from ChatGPT completions, I always force it to use JSON, which works perfectly well.

Assistants, however, cannot be forced to respond with JSON unless they only use functions, no data retrieval. So I want to be lazy, just retrieve the last message from a thread and if it contains a JSON, my automation will take the right data and do the next steps with it. This way I don’t have to do much, just to trigger an automation every once in a while.

Looks like there is a bug with the global match set to no for some reason, and yes works.

First time I’m seeing this issue. Make might have implemented regex slightly differently from regex101.

Screenshot_2024-04-19_220403

2 Likes

Hey Sam, thank you for checking this in.

Is there anything I should do in this case? Should I raise this thread as a bug report somehow?

You could, but I don’t think it really affects anyone since nobody’s brought it up before.