I have a JSON string which I’m passing to a function.
The string is dynamically generated and may contain different “name” parameters.
It looks like this (formatted for easier reading):
<cfset x = "
series: [
{
name:inbounds,
data:
[
[Date.UTC(2012,4,1),0],
[Date.UTC(2012,4,2),0],
[Date.UTC(2012,4,3),0]
]
},
{
name:outbounds,
data:
[
[Date.UTC(2012,4,1),0],
[Date.UTC(2012,4,2),0],
[Date.UTC(2012,4,3),0]
]
},
{
name:api,
data:
[
[Date.UTC(2012,4,1),441],
[Date.UTC(2012,4,2),441],
[Date.UTC(2012,4,3),443]
]
},
{
name:excess,
data:
[
[Date.UTC(2012,4,1),0],
[Date.UTC(2012,4,2),0],
[Date.UTC(2012,4,3),0]
]
}
]
">
However, I need the value of the “name” parameter to have single quotes around it,
e.g. Instead of name:inbounds, I need name:'inbounds'
So, I need something that will search through this string, locate the name parameter and put single quotes around its value.
EDIT
THe reason I’m doing it this way is because after I call serializeJSON on an array, it generates the following (shorted for snapshot):
[{"NAME":"Excess","DATA":[["Date.UTC(2012,5,1)",0],......
CF puts everything in double quotes. THe Highcharts API I’m using doesn’t want it like this.
So, I get rid of the double quotes with
<cfset x = replace(x, """", "", "all")>
Then I need to add back single quotes on the string vars.
If there’s a better way, I’m open to it.
If you must do it with Regex then this should work