Convert Clojure data structure to JSON and parse it in PHP

JSON is an idea data exchange format between different applications. If the data is type of document, XML maybe better, for other cases, JSON is better. If you are generating data in Clojure application, and the results will be processed in a PHP application, the data format to share between them can be JSON.

We have a data structure in Clojure

 
[
 ["obj1" "obj2" "obj3"]
]
 

To convert it to JSON, use clojure.data.json/json-str

 
user> (print (clojure.data.json/json-str [["obj1" "obj2" "obj3"]]))
[["obj1","obj2","obj3"]]
nil
 

Other functions in clojure.data.json namespace.

 
user> (dir clojure.data.json)
-write
JSONWriter
json-str
pprint
pprint-json
print-json
read
read-json
read-str
write
write-json
write-str
nil
 

To parse the JSON string to PHP data structures, use json_decode

 
$jsonobject = json_decode($jsonstring)
print_r($jsonobject);
 
Array
(
    [0] => Array
        (
            [0] => obj1
            [1] => obj2
            [2] => obj3
        )
 
)
 

If json_decode return NULL, the best way to diagnostic is print out the JSON string and validate it with tools like JSON lint, there are many things can happen before the JSON string is passed to your code, for example other code may called htmlentities on the JSON string.