Getting a JSON object

I want to create object and get it in JSON format along with its ID in the format.

Suppose I create object like this:

CREATE (task3:TASK {idesc: 'Task 3', status: 'new', due_date: date("2020-06-16") })

The simplest query
MATCH (n) RETURN n
This returns a too complex nested structure. What I want is just the object I created above in JSON with auto-generated ID also as a parameter. Anyway, I peeked deep inside the nesting to find "records" and then "properties" of each element which is closest to what I want. Here is what I get:
{due_date: n, status: "pending", desc: "Task 1"}

This has 2 problems: I want due_date in UTC string format. I also want the ID of the object returned in an ID property.

Question 1) Is there an easy way to get what I want?

Question 2) I tried coding this query to get the id:
MATCH (n) RETURN {id:ID(n), desc: n.desc, status: n.status, due_date:n.due_date}

But here ID does not work and gives error: This record has no field with key 'n'.

If I do just
MATCH (n) RETURN ID(n)

Then ID works without problem. Why doesn't it work inside the earlier expression?

Thanks.

Hi @kanade.sanjay,
I tried to reproduce something similar but
MATCH (n) RETURN {id:id(n), name:n.name} worked for me.
For UTC format, have you considered using APOC library ?

Yes, it works fine in browser query but I get error from my JS client. I will look again to see if it is some client code problem. Thanks.

About using apoc to format date, I can try that. But I wish Neo4J had an option to return all fields as strings in the JSON object. For example, if I try to directly get n.due_date, it returns the string format correctly without doing anything. There should be an option to do the same for the nested object structure that it returns. Instead, it returns date by default in a complex nested structure which has no use on the client side.

You can export your query results to json with this APOC procedure. You have to enable this export feature in no4j.config file

CALL apoc.export.json.query(
"MATCH (c)-[r]-(d)-[r1]-(b)-[r2]-(f) 
with collect(distinct c) + collect(d) + collect(b) + collect(f) as nodes, 
collect(r) + collect(r1) + collect(r2) as relationships 
RETURN nodes, relationships", null, {stream: true}) YIELD data 

RETURN data

Date comes as string: createdOn:"2020-06-22"
1 Like