cancel
Showing results for 
Search instead for 
Did you mean: 

Parse JSON values

wanderer2019
Champ in-the-making
Champ in-the-making
Good day,

i am tryin to get values on alfresco side web script, sent by share web script.

Script looks very primitive, made just for testing purposes (located in C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\sample):
function main(){
logger.log("Starting to parse input params");
model.mytestval=JSON.get("testval");
logger.log("Finished to parse input params.");
}

main();

BUT, whenever i try to call it from dashlet i get exception telling that JSON is not defined.
What i do wrong? How come a lot of default scripts use JSON object and i got this exception?
6 REPLIES 6

mikeh
Star Contributor
Star Contributor
Is your JavaScript file called sample.get.json.js? The "json" part is important.

Thanks,
Mike

wanderer2019
Champ in-the-making
Champ in-the-making
Is your JavaScript file called sample.get.json.js? The "json" part is important.

Thanks,
Mike

Hello Mike,

yes, my script called get_nodes_by_category.post.json.js.

here is the listing of other files.
get_nodes_by_category.post.desc.xml
<webscript>
  <shortname>Get spaces by category</shortname>
  <description>Retrieve spaces in specified category</description>
  <url>/get_nodes_by_category</url>
  <format default="json">argument</format>
  <authentication>user</authentication>
  <transaction>required</transaction>
</webscript>

get_nodes_by_category.post.json.ftl
{
   "myval":"${mytestval}"
}

and the call script looks like this:
getsearchablenodes.get.js
var testarray = new Array(1);
testarray[0]="testval: valuepassedtoscript";

var connector = remote.connect("alfresco");
var data = connector.post("/get_nodes_by_category",jsonUtils.toJSONString(testarray), "application/json");

var results = eval('(' + data + ')');

model.body = results["myval"];

wanderer2019
Champ in-the-making
Champ in-the-making
More over, when i change code to this:

if (json.has("testval") == true){   
         logger.log("value found!!!");
      }

i receive exception:
Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot find function has.

wanderer2019
Champ in-the-making
Champ in-the-making
Can anybody guide me? what i do wrong?

mikeh
Star Contributor
Star Contributor
The problem is the JSON object you're creating the POSTing to the webscript.

This:
var testarray = new Array(1);
testarray[0]="testval: valuepassedtoscript";
connector.post("/get_nodes_by_category",jsonUtils.toJSONString(testarray), "application/json");
Does not produce a top-level JSON object called "testval".

Try (untested):
var test =
{
   testval: "valuepassedtoscript"
};
connector.post("/get_nodes_by_category",jsonUtils.toJSONString(test), "application/json");

Thanks,
Mike

wanderer2019
Champ in-the-making
Champ in-the-making
Thats solved a problem.
Thanks!