Converting JSON string to an array in ColdFusion MX7

761 views Asked by At

I have a cookie value like:

"[{"index":"1","name":"TimePeriod","hidden":false},{"index":"2","name":"Enquiries","hidden":false},{"index":"3","name":"Online","hidden":false}]"

I would like to use this cookie value as an array in ColdFusion. What would be the best possible way to do this?

2

There are 2 answers

1
Sam Farmer On

JSON serialization was added natively in CF8.

If you are on MX7 look on riaforge.org for a library that will deSerialize JSON for you.

0
Peter Boughton On

The normal answer would be use the built-in deserializeJson function, but since that function wasn't available in CFMX7 (it arrived in CF8), you will need to use a UDF to achieve the same thing.

There are two sites which contain resources of this type, cflib.org and riaforge.org, each of which have a different potential solution for MX7.

Searching CFlib provides JsonDecode. (CFLib has a specific filter for "Maximum Required CF Version", so you can ensure any results that appear will work for your version.)

Searching riaforge provides JSONUtil, which runs on MX7 (but also claims better type mapping than the newer built-in functions).

Since MX7 runs on Java, you can likely also make use of any of the numerous Java libraries listed on json.org, using createObject/java.