0

I am using ColdFusion to Ajax data from a cfc and returning the data serialized. The console shows the data in this format.

query('id':[1],'title':['value'],'descr':['value2']) 

How do I parse this?

glomad
  • 5,539
  • 2
  • 24
  • 38
dutchlab
  • 541
  • 1
  • 13
  • 27
  • 6
    If you return JSON, then you can easily parse it with JS. But the format you show is custom, so you'd have to write your own parser. Note that this has nothing to do with jQuery or JSON. – Felix Kling Aug 26 '13 at 13:38
  • Since the format you show is similar to JSON, it might be easiest to change your ColdFusion code to return JSON, so you can use [jQuery’s JSON parsing function](http://api.jquery.com/jQuery.parseJSON/). If I guess at how to read your custom format, the equivalent JSON to your example would be `{'id':1,'title':'value','descr':'value2'}`. – Rory O'Kane Aug 26 '13 at 13:56
  • The custom format is actually a coldfusion function you can use to serialize the query. I am going to use the serializeJSON and see how that works. – dutchlab Aug 26 '13 at 14:01
  • {"COLUMNS":["ID","TITLE","DESCR"],"DATA":[[1,"value1","value2"]]}. Using the serializeJSON the console shows this. I have seen where some people have had to write specials scripts to get the data into a different format. Is there a way to just handle this with JQuery? – dutchlab Aug 26 '13 at 14:06
  • @user1088014: http://stackoverflow.com/q/4935632/218196. Then you can do whatever you want with the data. – Felix Kling Aug 26 '13 at 14:08

2 Answers2

1

This is a custom format which isn't easy to parse but with a few changes, you can convert it to JSON:

  1. Replace query( with {
  2. Replace the tailing ) with }
  3. Replace " with \"
  4. Replace ' with "

If you want to keep query, then replace it with {"query":{ and replace ) with }}

Note: This is a very simple and therefore brittle solution. A better solution would be to get your server side framework to produce proper JSON and send that to the browser.

EDIT If you have a JSON string, you can use jQuery.parseJSON() to parse it.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • The custom format is actually a coldfusion function you can use to serialize the query. I am going to use the serializeJSON and see how that works. – dutchlab Aug 26 '13 at 14:06
  • {"COLUMNS":["ID","TITLE","DESCR"],"DATA":[[1,"value1","value2"]]}. Using the serializeJSON the console shows this. I have seen where some people have had to write specials scripts to get the data into a different format. Is there a way to just handle this with JQuery? – dutchlab Aug 26 '13 at 14:07
  • Yes: `var obj = jQuery.parseJSON(...);` – Aaron Digulla Aug 26 '13 at 14:23
0

The format your data is serialized makes parsing it to JSON really easy. See the jsfiddle for a live example. I started with getting the raw data as a string. Next I replaced the single quotes and added curly brackets around the string to fit the JSON string format.

jsFiddle example


Step 1

query('id':[1],'title':['value'],'descr':['value2']) 

to

'id':[1],'title':['value'],'descr':['value2']

Step 2

'id':[1],'title':['value'],'descr':['value2']

to

{"id":[1],"title":["value"],"descr":["value2"]}

Now you can use JSON.parse to create a javascript object from the serialized string. But note that all values in your format are array values. This is a little example to show what I mean:

{ 
    "int": 1, 
    "boolean": true, 
    "string": "foo", 
    "array": ["value1",  true, 1234] 
}

In your example every value is an array value, but JSON.parse parsed them as single values if the array contains only one element.

naloxx
  • 231
  • 1
  • 7