Need to return streamlined json from query

I have a javascript app that queries data from neo4j

I want to return only the id and name properties from the "Tag" node. this is my query:

MATCH (t:Tag)
RETURN { id: t.id, name: t.name } AS tag
ORDER BY toLower(tag.name) ASC

The query is running fine, but when I convert the results to json like this:

console.log(JSON.stringify(result.records));

My json looks like this:

[
{
"keys":[
"tag"
],
"length":1,
"_fields":[
{
"name":"Banking",
"id":"e26fbed3-e326-4744-8459-766d59d9b95f"
}
],
"_fieldLookup":{
"tag":0
}
},
{
"keys":[
"tag"
],
"length":1,
"_fields":[
{
"name":"BI",
"id":"c15bc69 d-0892-4f24-b714-bd1f8ac3ee22"
}
],
"_fieldLookup":{
"tag":0
}
},
... etc.

its returning a lot of text I don't need and will create extra traffic across the intenet when I fetch a large number of nodes.

What can I do to only return json that looks like this:

[
{
"name":"Banking",
"id":"e26fbed3-e326-4744-8459-766d59d9b95f"
},
{
"name":"BI",
"id":"c15bc69 d-0892-4f24-b714-bd1f8ac3ee22"
},
... etc.

Thank you

I assume it is because you are processing the entire record. Instead, get the “tag” attribute first from each record using get(”tag”) on each record.

Can you please share with me what that would look like in the query?

Thanks.

1 Like

Hi Gary,

Also interested - Can you please share with me what that would look like in the query?

Thanks.

When you execute JSON.stringify(result.records) you are serializing the 'records' object from a list. Each record apparently has additional metadata to describe the record in addition to the you returned as 'tag'.

If you want to look at just your data from each record, try something like this:

result.records.forEach(record => {
      console.log(JSON.stringify(record.get('tag')))
    })