Javascript function - how to parse query result ?

61 views
Skip to first unread message

Suyog Kale

unread,
Aug 17, 2016, 5:15:12 AM8/17/16
to OrientDB
Hello,

I am create javascript function using studio.

var result  = q.command( "sql",mySQLCommand);


here result is below json:

[
    {
        "@type": "d",
        "@rid": "#-2:1",
        "@version": 0,
        "vKRId": "#23:0",
        "uRId": "#21:0",
        "@fieldTypes": "vKRId=x,uRId=x"
    }
]

I want read vKRId and uRId values from result variable. I tried using JSON.parse or JSON.stringfy, it gives below error
SyntaxError: Invalid JSON: <json>:1:0 Expected json literal but found

What is best practice to parse query result?

alessand...@gmail.com

unread,
Aug 17, 2016, 7:03:31 AM8/17/16
to OrientDB
Hi,
you can use getProperty("yourproperty")

Example

var g=orient.getGraph();
var b=g.command("sql","select from persona");
for(i=0;i<b.length;i++){
 
var record=b[i];
 
var id=record.getId();
 
var name=record.getProperty("name");
 
print(id + name);
}

Best regards,
Alessandro

Suyog Kale

unread,
Aug 18, 2016, 2:01:09 AM8/18/16
to OrientDB
Thanks Alessandro,
I tried with the approach you have mentioned, but gives me whole vertex. below is screenshot:





Please suggest,
Auto Generated Inline Image 1

Suyog Kale

unread,
Aug 18, 2016, 2:17:36 AM8/18/16
to OrientDB
Hi Alessandro,

I did few changes in select queries.

now I get following result using getProperty:

[
    {
        "@type": "d",
        "@version": 0,
        "value": "#-2:0--v(Employee)[#23:0]--v(Post)[#21:0]"
    }
]


here is my code:

var record=getlikeKloojjEdge[0];
var id=record.getId();
var name=record.getProperty("EMPRID")+"--"+record.getProperty("postRID");
return(id+"--"+name);

Why it concatenating vertex names as prefix? please suggest.

alessand...@gmail.com

unread,
Aug 18, 2016, 4:08:24 AM8/18/16
to OrientDB
Hi,
you could use 

var name=record.getProperty("EMPRID").getId()+"--"+record.getProperty("postRID").getId();




Best regards,
Alessandro

Reply all
Reply to author
Forward
0 new messages