Access JSON parameters inside Javascript fuction

108 views
Skip to first unread message

normanLinux

unread,
Jun 23, 2015, 3:54:34 PM6/23/15
to orient-...@googlegroups.com
I am in the early stages with orientdb.  As part of my preparation I am trying to set up functions to handle POST input from Angular (where the data will be sent in JSON format).

I want to be able to access fields within the object, but have been unable to find how to unpack it.

My test function is deliberately very simple.  In studio I create a function that takes  single parameter: record

Function contents is:-
var g=orient.getGraph(); // not needed for this test
var r = record;
var rid = r.rid;
var v = r.val;
return r;
=======
record contains:
{rid:123,val:{name:"Norman",phone:"Home"}}

return value is:
[
    {
        "@type": "d",
        "@version": 0,
        "value": "{rid:123,val:{name:\"Norman\",phone:\"Home\"}}"
    }
]
======================
However, if I return rid (or v), I get nothing.

Noting that the returned value, r, is an array containing a single object I tried to return r[0] in the hope of accessing its members
response was:
[
    {
        "@type": "d",
        "@version": 0,
        "value": "{"
    }
]
I have looked anywhere that I can think of but have obviously missed the answer.
Could somebody please help me to see what I've got wrong?
N.B. I have tried the keys in my object both with and without quotes - same result.

normanLinux

unread,
Jun 23, 2015, 4:31:17 PM6/23/15
to orient-...@googlegroups.com
This seems to  be a very similar problem to that posted by finn:-
Function: can not read field value from the returned object of a select query

Roger Gajraj

unread,
Jun 23, 2015, 6:16:02 PM6/23/15
to orient-...@googlegroups.com
try getProperty function.

Like so:  r.getProperty('rid')

alessand...@gmail.com

unread,
Jun 24, 2015, 4:45:45 AM6/24/15
to orient-...@googlegroups.com
Hi,
you can't get rid with r.getProperty('rid') because rid isn't a property but it is a metadata
you can try this code   var rid=r.getId().toString();
                                  return rid;

Regards,
Alessandro

normanLinux

unread,
Jun 24, 2015, 6:15:39 AM6/24/15
to orient-...@googlegroups.com
Thank you for these helpful suggestions - and I believe that they will be useful for parsing records returned from a query.

My problem is that I want to be able to parse the input parameters to a function and make decisions based on the content of different fields.

For some reason I am unable to extract individual fields from my object - even though your Mozilla-based JavaScript engine does see it as an object. 
When I try Roger's suggetion I get:
Erroronparsingscriptatposition#0: ErroronexecutionofthescriptScript: addFact------^sun.org.mozilla.javascript.internal.EcmaError: TypeError: CannotfindfunctiongetPropertyinobject{
    rid: 123,
    val: {
        name: "Norman",
        phone: "Home"
    }
}.(<Unknownsource>#7)in<Unknownsource>atlinenumber7TypeError: CannotfindfunctiongetPropertyinobject{
    rid: 123,
    val: {
        name: "Norman",
        phone: "Home"
    }
}.(<Unknownsource>#7)
With a similar message if I try Alessandro's suggestion.  (Luckily for me, both of these answers are useful in other circumstances, though!)

alessand...@gmail.com

unread,
Jun 24, 2015, 6:29:10 AM6/24/15
to orient-...@googlegroups.com
Hi Norman,

can you post all the code of your function, the parameters ( and the types) and your schema 

Alessandro

normanLinux

unread,
Jun 25, 2015, 7:07:25 AM6/25/15
to orient-...@googlegroups.com
Hi, 

My original post contained all of the code for the function - plus the (simple) input used with it.  
As you see, I am - at present - simply trying to see how to split out fields from an input parameter sent as an object via JSON, so my test data is a simplified object.  
At this stage the schema is irrelevant.  I just want to test sending a JSON object and handling fields from it in my functions.

alessand...@gmail.com

unread,
Jun 25, 2015, 8:34:17 AM6/25/15
to orient-...@googlegroups.com
Hi Norman,

try this function with the parameter record = {"rid":"123","val":{"name":"Norman","phone":"Home"}}

var obj = JSON.parse(record);
var rid=  obj.rid;
var val = obj.val;
var name= val.name;
var phone= val.phone;
return name;

Regards,
Alessandro


normanLinux

unread,
Jun 25, 2015, 10:22:25 AM6/25/15
to orient-...@googlegroups.com
Thank you.

nearly there!
but this returns
[
    {
        "@type": "d",
        "@version": 0,n I pass the
        "value": "Norman"
    }
]
How do I access the value (Norman) from within the new returned object?  - or can I pass the whole returned record as a value for, say, an update?
Message has been deleted

alessand...@gmail.com

unread,
Jun 25, 2015, 10:37:12 AM6/25/15
to orient-...@googlegroups.com
Hi Norman,
if you try with broswe you can get only the value Norman



Alessandro

normanLinux

unread,
Jun 25, 2015, 11:15:57 AM6/25/15
to orient-...@googlegroups.com
Great, I'll check that tomorrow as I'm away at present.  Thanks!

alessand...@gmail.com

unread,
Jun 25, 2015, 11:27:21 AM6/25/15
to orient-...@googlegroups.com
Hi Norman,
I think that being the format JSON, when you return the object you will have also the fields @type and @version.

Alessandro

normanLinux

unread,
Jun 26, 2015, 7:44:25 AM6/26/15
to orient-...@googlegroups.com
I get that I will receive back those fields in my application - not a problem.  However, accessing fields from a JSON parameter object within a function is where I am having trouble.
It may help if I explain what I want to do.

I don't want to pollute my client-side with details of the database operations, so I want to be able to pass parameters like this:
{"from_rid": "the rid","to_rid": "the rid", "edge_type": "type of edge to create","edge_properties":{"property":"value", . . .}}

Then, at the Orient end, I want the function to unpack that and create the appropriate edge

BTW, I am currently running 2.0.8

alessand...@gmail.com

unread,
Jun 26, 2015, 9:49:15 AM6/26/15
to orient-...@googlegroups.com
Hi Norman,
try this function with the parameter record {"from_rid": "#12:0","to_rid": "12:1", "edge_type": "friend","edge_properties":{"property1":"value_property", "property2":"value2_property"}}

var obj = JSON.parse(record);
var ridFrom=  obj.from_rid;
var ridTo = obj.to_rid;
var edge_type= obj.edge_type;
var edge_properties= obj.edge_properties;
var property1=edge_properties.property1;
var property2=edge_properties.property2;
var set=" set "+Object.keys(edge_properties)[0] + " = '" + property1 + "', " + 
    Object.keys(edge_properties)[1] + " = '" + property2 + "'"
var query="create edge "+ edge_type + " from " + ridFrom + " to " + ridTo + set ;
var g=orient.getGraph();
g.command('sql',query);

Alessandro

normanLinux

unread,
Jun 26, 2015, 11:03:46 AM6/26/15
to orient-...@googlegroups.com
Fantastic!

Exactly what I need - and it will enable me to create other, similar functions!
Reply all
Reply to author
Forward
0 new messages