?
Solved

Coldfusion remove square brackets from string

Posted on 2016-08-08
4
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 41747219
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
ID: 41747295
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 2000 total points
ID: 41747337
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
ID: 41747378
Thank you very much agx.
Problem is solved.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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 …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 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