AnsweredAssumed Answered

jsonUtils usage

Question asked by dlamoris on Aug 6, 2013
Latest reply on Aug 7, 2013 by dlamoris
Hi all,

I'm creating a repository tier webscript that processes content posted as json. In the script, I got the root 'json' object ok, but when I try to convert it to a native js object using jsonUtils.toObject(json), I can't figure out what's going on…

example posted json:
{"a": [{"b": "c"}, {"d": "e"}]}

when i do:
<javascript>
var test = jsonUtils.toObject(json);
</javascript>

Using the alfresco javascript debugger and the Expression window, here's what I see:

json                      {"a": [{"b": "c"}, {"d": "e"}]}
test                      TypeError: Cannot find default value for object.
test.a                    [{"b": "c"}, {"d": "e"}]
test.a.length         
test.a.size               function size() {/* int size() */}
test.a.get                function get() {/* java.lang.Object get(int) */}
test.a[0]                 Java class "org.json.simple.JSONArray" has no public instance field or method named "0".
test.a.get(0)             null
test.a.getJSONObject(0)   TypeError: Cannot find function getJSONObject. 

This is confusing enough. From the documentation it seems that jsonUtils.toObject should return a native javascript object, except array values were not converted, and I can't even use functions from JSONArray java object? I'm struggling to iterate over the array that's the value of "a".

(I also tried to import and use the json2.js file at https://github.com/douglascrockford/JSON-js instead, but it failed to import and choked when trying to add the toJSON function to Date.prototype)

Any help is appreciated!

Outcomes