Solved

Coldfusion remove square brackets from string

Posted on 2016-08-08
4
142 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending Text Messages 1 62
Coldfusion get content of HTML table 8 216
Coldfusion: Two dynamic depending drop down 12 43
CSS HELP 6 93
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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