Solved

Coldfusion remove square brackets from string

Posted on 2016-08-08
4
64 Views
Last Modified: 2016-08-08
Hello experts.
I need help to remove square brackets from a string
Any help?
0
Comment
Question by:Panos
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
If you just want to remove the brackets, a basic replace() would work:

<cfset theText = "This is [some text] with [brackets]">
<cfset newText =  Replace(Replace(theText, ']', '', 'all'), '[', '', 'all')>
newText = <cfoutput>#newText#</cfoutput>

Open in new window


Otherwise, can you give an example of the original text, and desired result?
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Hi agx.
From my last question i have a binary response from <cfhttp call and using charsetEncode(response.filecontent, "utf-8")>  i have output like this:
{"product":[{"productId":"87","productname":"Name1"},{"productId":"40","productname":"Name2"},{"productId":"486","productname":"Name3"}],"keyMap":"products:shampoo"}

I m trying to get only the list of products and loop over the list and insert the values in my database.
Using the rereplace i did delete the '{"product": '  and  ',"keyMap":"products:shampoo"}''
But wth the brackets it didn t work. i got the error:
 Malformed regular expression "[".
Reason: Unmatched [] in expression..
error line: <cfset responseString = rereplace(responseString,"[","","ALL")/>

I don t know  if is the right way to do this. I m sure there is a better way.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
Edit:  Oh okay. Forget about the replace. The response string is JSON. Just deserialize the whole string and you'll get back CF objects you can loop through:

<!--- DEMO ONLY --->
<cfsavecontent variable="responseString">{"product":[{"productId":"87","productname":"Name1"},{"productId":"40","productname":"Name2"},{"productId":"486","productname":"Name3"}],"keyMap":"products:shampoo"}</cfsavecontent>

<cfset response = deserializeJSON(responseString)>
<cfdump var="#response#">

<!--- the "product" key contains an array of structures (with product details) --->
<cfloop array="#response.product#" index="product">
	<cfoutput>
        <!--- DEMO: display ID and product name --->
        <!--- Do whatever you want with the id's and names here --->
	product = #product.productid# - #product.productName#<br>
	</cfoutput>
</cfloop>

Open in new window

0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Thank you very much agx.
Problem is solved.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now