Copy link to clipboard
Copied
I am running a cfhttp request as follows:
<cfhttp url="http://www.thedata.place/dataset/323f337f-8d97-4e39-acf4-dbdb7a7a3fab/resource/911510a9-bd85-409c-bc..." >
<!--- JSON data is sometimes distributed as a JavaScript function.
The following REReplace functions strip the function wrapper. --->
<cfset theData = REReplace(cfhttp.FileContent, "^\s*[[:word:]]*\s*\(\s*","")>
<cfset theData = REReplace(theData, "\s*\)\s*$", "")>
<cfset cfData = DeserializeJSON(theData)>
<cfdump var="#theData#" >
This results in:
{ "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, "features": [ { "type": "Feature", "properties": { "LIBNAME": "Crownhill Library", "ADD1": "Cross Park Road", "ADD2": "Crownhill", "ADD3": "Plymouth", "PCODE": "PL6 5AN", "TEL": "01752 306248", "EMAIL": "Crownhill.Library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/crownhilllibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.132955965168074, 50.406718514326919 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Devonport Library", "ADD1": "Welcome Hall", "ADD2": "Fore Street", "ADD3": "Plymouth", "PCODE": "PL1 4DP", "TEL": "01752 305203", "EMAIL": "Devonport.Library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/devonportlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.174697018052764, 50.372827940532417 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Efford Library", "ADD1": "19 Torridge Way", "ADD2": "Efford", "ADD3": "Plymouth", "PCODE": "PL3 6JQ", "TEL": "01752 308985", "EMAIL": "Efford.Library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/effordlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.110040307590939, 50.388079925877328 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Eggbuckland Library", "ADD1": "Eggbuckland Community College, Westcott Close", "ADD2": "Eggbuckland", "ADD3": "Plymouth", "PCODE": "PL6 5YB", "TEL": "01752 786353", "EMAIL": "library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/eggbucklandlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.122846665154559, 50.39997987154797 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Ernesettle Library", "ADD1": "3 Hornchurch Road", "ADD2": "Ernesettle", "ADD3": "Plymouth", "PCODE": "PL5 2TQ", "TEL": "01752 365522", "EMAIL": "Ernesettle.Library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/ernesettlelibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.179056232722283, 50.418787316598078 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Estover Library", "ADD1": "Tor Bridge High School, Miller Way", "ADD2": "Estover", "ADD3": "Plymouth", "PCODE": "PL6 8UN", "TEL": "01752 302502", "EMAIL": "estover.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/estoverlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.100470326558343, 50.411525229608934 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Laira Library", "ADD1": "240 Old Laira Road", "ADD2": "Laira", "ADD3": "Plymouth", "PCODE": "PL3 6AF", "TEL": "01752 665737", "EMAIL": "laira.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/lairalibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.106933464527195, 50.383632173669646 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "North Prospect Library", "ADD1": "163 North Prospect Road", "ADD2": "North Prospect", "ADD3": "Plymouth", "PCODE": "PL2 2ND", "TEL": "01752 398138", "EMAIL": "northprospect.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\r\northprospectlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.164283586221993, 50.388021365334723 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Peverell Library", "ADD1": "242a Peverell Park Road", "ADD2": "Peverell", "ADD3": "Plymouth", "PCODE": "PL3 4QF", "TEL": "01752 305633", "EMAIL": "peverell.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/peverelllibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.145326106515149, 50.393963077714297 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Plympton Library", "ADD1": "Ridgeway", "ADD2": "Plympton", "ADD3": "Plymouth", "PCODE": "PL7 2AS", "TEL": "01752 305630", "EMAIL": "Plympton.Library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/plymptonlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.051702307546147, 50.387879564292582 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Plymstock Library", "ADD1": "Horn Cross Road", "ADD2": "Plymstock", "ADD3": "Plymouth", "PCODE": "PL9 9BU", "TEL": "01752 306606", "EMAIL": "Plymstock.Library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/plymstocklibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.088128950729001, 50.359990687654388 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Southway Library", "ADD1": "351 Southway Drive", "ADD2": "Southway", "ADD3": "Plymouth", "PCODE": "PL6 6QR", "TEL": "01752 305636", "EMAIL": "southway.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/southwaylibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.125622609595956, 50.429498444324999 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "St Budeaux Library", "ADD1": "The Square, Victoria Road", "ADD2": "St Budeaux", "ADD3": "Plymouth", "PCODE": "PL5 1RQ", "TEL": "01752 306237", "EMAIL": "stbudeaux.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/stbudeauxlibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.186906015481138, 50.402964958484922 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Stoke Library", "ADD1": "21 Albert Road", "ADD2": "Stoke", "ADD3": "Plymouth", "PCODE": "PL2 1AB", "TEL": "01752 306239", "EMAIL": "stoke.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/stokelibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.170958831568073, 50.379008467120052 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Tothill Library", "ADD1": "Tothill Community Centre, Knighton Road", "ADD2": "St Judes", "ADD3": "Plymouth", "PCODE": "PL4 9DA", "TEL": "01752 667427", "EMAIL": "tothill.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/tothilllibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.119046670061567, 50.373587405862175 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "West Park Library", "ADD1": "423 -425 Crownhill Road", "ADD2": "Honicknowle", "ADD3": "Plymouth", "PCODE": "PL5 2LJ", "TEL": "01752 306241", "EMAIL": "westpark.library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/westparklibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.166488144592547, 50.412585857276191 ] ] } }, { "type": "Feature", "properties": { "LIBNAME": "Central Library", "ADD1": "Armada Way", "ADD2": "City Centre", "ADD3": "Plymouth", "PCODE": "PL4 8AL", "TEL": "01752 305912", "EMAIL": "library@plymouth.gov.uk", "WEB": "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/centrallibrary\/lendinglibrary.htm", "NOTES": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -4.137880281630226, 50.374063170919605 ] ] } } ] }
I have searched the internet on DeSerializeJSON() to try and make sense of the above but am getting nowhere. How would I turn it into a query or read it so that I can get the information out into a nice formatted way - or if all I wanted to do is get the coordinates out so I could plot a map - how would I do that - I am guessing it needs to be a query of sorts.
All the answers I have found get me errors like "Complex structures" or "Runtime".
Many thanks for helping in advance - this isn't the only one I have to try and make head or tail out of!! So advice also on how different types of structures like the above are read would be really useful.
J
Copy link to clipboard
Copied
Hi, wearekonstruct,
DeserializeJSON() can be used to turn a JSON object into a structure, array, or query object. If the data originally was a query object and you used SerializeJSON() to create the JSON object, depending upon certain arguments used you can automatically revert the JSON object back into a query object. Useful tool if you need to pass a lot of data via form or web service.
If the JSON object was not originally a CF object, you can still make a structure or array or query from it, but you have to manipulate the string because CF formats JSON slightly differently.
I don't have any sample code, at the moment, but I have (in the past) used Serialize/DeserializeJSON, and it can be quite useful.
HTH,
^_^
UPDATE: Let me clarify, a bit. If the JSON string/object was originally a CF query, then you would use:
<cfset cfData = DeserializeJSON(theData,false) />
The 'false' argument for the useStrictMapping argument tells CF Server that if the original CF object converted to JSON was a query, make this a query.
If the original object was _not_ a CF object, then you need to format the JSON string/object in such a way that CF can convert it to a CF object.
Copy link to clipboard
Copied
Your data is complex. It has structure, but you have to tidy it up.
Some points to take into account:
1) { } represents a structure in ColdFusion. Example: {someKey="someValue"} instead of {"someKey":"someValue"}.
Use regular expressions to replace all occurrences of '"someKey": ' with 'someKey= '.
Example:
<cfset testString = '{"key1":"value1","key2":"value2"}'>
<cfset newTxt = testString.replaceAll('"(\b[a-zA-Z0-9]+\b)":', '$1=')>
<cfoutput>#newTxt#</cfoutput>
2) [ ] represents an array. Example: [1, "some words", someObject].
3) The following may be troublesome in ColdFusion: "someKey"= null.
Acceptable alternatives are: someKey= "" or someKey= "null".
After you implement these suggestions you will get the following structure:
<cfset theData= {
type= "FeatureCollection",
crs= { type= "name", properties= { name= "urn:ogc:def:crs:OGC:1.3:CRS84" } },
features=
[
{
type= "Feature",
properties= { LIBNAME= "Crownhill Library", ADD1= "Cross Park Road", ADD2= "Crownhill", ADD3= "Plymouth", PCODE= "PL6 5AN", TEL= "01752 306248", EMAIL= "Crownhill.Library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/c rownhilllibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.132955965168074, 50.406718514326919 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Devonport Library", ADD1= "Welcome Hall", ADD2= "Fore Street", ADD3= "Plymouth", PCODE= "PL1 4DP", TEL= "01752 305203", EMAIL= "Devonport.Library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/d evonportlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.174697018052764, 50.372827940532417 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Efford Library", ADD1= "19 Torridge Way", ADD2= "Efford", ADD3= "Plymouth", PCODE= "PL3 6JQ", TEL= "01752 308985", EMAIL= "Efford.Library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/e ffordlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.110040307590939, 50.388079925877328 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Eggbuckland Library", ADD1= "Eggbuckland Community College, Westcott Close", ADD2= "Eggbuckland", ADD3= "Plymouth", PCODE= "PL6 5YB", TEL= "01752 786353", EMAIL= "library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/e ggbucklandlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.122846665154559, 50.39997987154797 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Ernesettle Library", ADD1= "3 Hornchurch Road", ADD2= "Ernesettle", ADD3= "Plymouth", PCODE= "PL5 2TQ", TEL= "01752 365522", EMAIL= "Ernesettle.Library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/e rnesettlelibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.179056232722283, 50.418787316598078 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Estover Library", ADD1= "Tor Bridge High School, Miller Way", ADD2= "Estover", ADD3= "Plymouth", PCODE= "PL6 8UN", TEL= "01752 302502", EMAIL= "estover.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/e stoverlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.100470326558343, 50.411525229608934 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Laira Library", ADD1= "240 Old Laira Road", ADD2= "Laira", ADD3= "Plymouth", PCODE= "PL3 6AF", TEL= "01752 665737", EMAIL= "laira.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/l airalibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.106933464527195, 50.383632173669646 ] }
},
{
type= "Feature",
properties= { LIBNAME= "North Prospect Library", ADD1= "163 North Prospect Road", ADD2= "North Prospect", ADD3= "Plymouth", PCODE= "PL2 2ND", TEL= "01752 398138", EMAIL= "northprospect.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\r\ northprospectlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.164283586221993, 50.388021365334723 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Peverell Library", ADD1= "242a Peverell Park Road", ADD2= "Peverell", ADD3= "Plymouth", PCODE= "PL3 4QF", TEL= "01752 305633", EMAIL= "peverell.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/p everelllibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.145326106515149, 50.393963077714297 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Plympton Library", ADD1= "Ridgeway", ADD2= "Plympton", ADD3= "Plymouth", PCODE= "PL7 2AS", TEL= "01752 305630", EMAIL= "Plympton.Library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/p lymptonlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.051702307546147, 50.387879564292582 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Plymstock Library", ADD1= "Horn Cross Road", ADD2= "Plymstock", ADD3= "Plymouth", PCODE= "PL9 9BU", TEL= "01752 306606", EMAIL= "Plymstock.Library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/p lymstocklibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.088128950729001, 50.359990687654388 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Southway Library", ADD1= "351 Southway Drive", ADD2= "Southway", ADD3= "Plymouth", PCODE= "PL6 6QR", TEL= "01752 305636", EMAIL= "southway.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/s outhwaylibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.125622609595956, 50.429498444324999 ] }
},
{
type= "Feature",
properties= { LIBNAME= "St Budeaux Library", ADD1= "The Square, Victoria Road", ADD2= "St Budeaux", ADD3= "Plymouth", PCODE= "PL5 1RQ", TEL= "01752 306237", EMAIL= "stbudeaux.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/s tbudeauxlibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.186906015481138, 50.402964958484922 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Stoke Library", ADD1= "21 Albert Road", ADD2= "Stoke", ADD3= "Plymouth", PCODE= "PL2 1AB", TEL= "01752 306239", EMAIL= "stoke.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/s tokelibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.170958831568073, 50.379008467120052 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Tothill Library", ADD1= "Tothill Community Centre, Knighton Road", ADD2= "St Judes", ADD3= "Plymouth", PCODE= "PL4 9DA", TEL= "01752 667427", EMAIL= "tothill.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/t othilllibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.119046670061567, 50.373587405862175 ] }
},
{
type= "Feature",
properties= { LIBNAME= "West Park Library", ADD1= "423 -425 Crownhill Road", ADD2= "Honicknowle", ADD3= "Plymouth", PCODE= "PL5 2LJ", TEL= "01752 306241", EMAIL= "westpark.library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/w estparklibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.166488144592547, 50.412585857276191 ] }
},
{
type= "Feature",
properties= { LIBNAME= "Central Library", ADD1= "Armada Way", ADD2= "City Centre", ADD3= "Plymouth", PCODE= "PL4 8AL", TEL= "01752 305912", EMAIL= "library@plymouth.gov.uk", WEB= "http:\/\/www.plymouth.gov.uk\/homepage\/leisureandtourism\/libraries\/findyourlibrary\/c entrallibrary\/lendinglibrary.htm", NOTES= "null" },
geometry= { type= "MultiPoint", coordinates= [ -4.137880281630226, 50.374063170919605 ] }
}
]
}>
<cfdump var="#theData#">
This tells you, for example, that
coordinates is an array
within a structure named geometry
within within an unnamed structure
within an array named features
within a structure named theData