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
Solved

Coldfusion remove square brackets from string

Posted on 2016-08-08
4
124 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_
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 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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