4

I don't understand how to get standard JSON back from an orientjs query. I see people talking about "serializing" the result, but I don't understand why or how to do that. There is a toJSON() method, but i only see it being used with fetchplans etc...

I am trying to pipe a stream to a csv file and it isn't working properly because of the incorrect JSON format.

I would love an explanation of how and when to serialize. :-)

My Query:

  return db.query(
    `SELECT 
      id,
      name,
      out('posted_to').name as page,
      out('posted_to').id as page_id,
      out('posted_to').out('is_language').name as language,
      out('posted_to').out('is_network').name as network 

    FROM post

    WHERE posted_at
      BETWEEN
        '${since}'
      AND
        '${until}'

      UNWIND 
        page,
        page_id,
        language,
        network
    `

My Result:

[ { '@type': 'd',
    id: '207109605968597_1053732754639607',
    name: '10 maneiras pelas quais você está ferindo seus relacionamentos',
    page: 'Eu Amo o Meu Irmão',
    page_id: '207109605968597',
    language: 'portuguese',
    network: 'facebook',
    '@rid': { [String: '#-2:1'] cluster: -2, position: 1 },
    '@version': 0 },
  { '@type': 'd',
    id: '268487636604575_822548567865143',
    name: '10 maneiras pelas quais você está ferindo seus relacionamentos',
    page: 'Amo meus Filhos',
    page_id: '268487636604575',
    language: 'portuguese',
    network: 'facebook',
    '@rid': { [String: '#-2:3'] cluster: -2, position: 3 },
    '@version': 0 }]
kurtcorbett
  • 1,355
  • 1
  • 12
  • 17
  • kurtcorbett - Have you found an answer? If so, please don't forget to post it here; otherwise, if you haven't already done so, consider creating a ticket at https://github.com/orientechnologies/orientdb/issues. Meanwhile, here is a workaround using jsonlint, and assuming the explicit String representation of @rid is unnecessary: sed "s/\[String: '#[-0-9:]*']//" | jsonlint -Sf – peak Jan 06 '16 at 20:13

2 Answers2

0

This is my dataset:

enter image description here

Query:

db.select('id','code').from('tablename').where({deleted:true}).all()
    .then(function (vertex) {
        console.log('Vertexes found: ');
        console.log(vertex);
});

Output:

Vertexes found:
[ { '@type': 'd',
    id: '6256650b-f5f2-4b55-ab79-489e8069b474',
    code: '4b7d99fa-16ed-4fdb-9baf-b33771c37cf4',
    '@rid': { [String: '#-2:0'] cluster: -2, position: 0 },
    '@version': 0 },
  { '@type': 'd',
    id: '2751c2a0-6b95-44c8-966a-4af7e240752b',
    code: '50356d95-7fe7-41b6-b7d9-53abb8ad3e6d',
    '@rid': { [String: '#-2:1'] cluster: -2, position: 1 },
    '@version': 0 } ]

If I add the instruction JSON.stringify():

Query:

db.select('id','code').from('tablename').where({deleted:true}).all()
    .then(function (vertex) {
        console.log('Vertexes found: ');
        console.log(JSON.stringify(vertex));
});

Output:

Vertexes found:
[{"@type":"d","id":"6256650b-f5f2-4b55-ab79-489e8069b474","code":"4b7d99fa-16ed-
4fdb-9baf-b33771c37cf4","@rid":"#-2:0","@version":0},{"@type":"d","id":"2751c2a0
-6b95-44c8-966a-4af7e240752b","code":"50356d95-7fe7-41b6-b7d9-53abb8ad3e6d","@ri
d":"#-2:1","@version":0}]

Hope it helps

LucaS
  • 1,418
  • 9
  • 12
0

I found a way that worked for me. instead of using :

db.query()

i used http request in node to query on database. on OrientDB Document also said you get only JSON format in result. this way if you query in database you will always get a valid JSON.

for making a http request i used request module. this is a sample that worked for me :

var request =  require("request");
var auth = "Basic " + new Buffer("root" + ":" + "root").toString("base64")

request(
        {
            url : encodeURI('http://localhost:2480/query/tech_graph/sql/'+queryInput+'/20'),
            headers : {
                "Authorization" : auth
            }
        },
        function (error, response, body) {
            console.log(body);
            return body;
        }
    );
soroush
  • 31
  • 7