On the Actor/Movie demo graph, cypher returns column names in a separate array.
MATCH (n:Person) RETURN n.name as Name, n.born as Born ORDER BY n.born LIMIT 5
results:
{ "columns" : [ "Name", "Born" ], "data" : [ [ "Max von Sydow", 1929 ], [ "Gene Hackman", 1930 ], [ "Richard Harris", 1930 ], [ "Clint Eastwood", 1930 ], [ "Mike Nichols", 1931 ] ]}
Is it possible to get each node properties tagged instead?
{ "nodes" : [ ["Name": "Max von Sydow", "Born": 1929 ], ...] }
If I return the node instead of selected properties, I get way too many properties.
MATCH (n:Person) RETURN n LIMIT 5
results:
{ "columns" : [ "n" ], "data" : [ [ { "outgoing_relationships" : "http://localhost:7474/db/data/node/58/relationships/out", "labels" : "http://localhost:7474/db/data/node/58/labels", "data" : { "born" : 1929, "name" : "Max von Sydow" }, "all_typed_relationships" : "http://localhost:7474/db/data/node/58/relationships/all/{-list|&|types}", "traverse" : "http://localhost:7474/db/data/node/58/traverse/{returnType}", "self" : "http://localhost:7474/db/data/node/58", "property" : "http://localhost:7474/db/data/node/58/properties/{key}", "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/58/relationships/out/{-list|&|types}", "properties" : "http://localhost:7474/db/data/node/58/properties", "incoming_relationships" : "http://localhost:7474/db/data/node/58/relationships/in", "extensions" : { }, "create_relationship" : "http://localhost:7474/db/data/node/58/relationships", "paged_traverse" : "http://localhost:7474/db/data/node/58/paged/traverse/{returnType}{?pageSize,leaseTime}", "all_relationships" : "http://localhost:7474/db/data/node/58/relationships/all", "incoming_typed_relationships" : "http://localhost:7474/db/data/node/58/relationships/in/{-list|&|types}" } ], ... ]}
You can use the new literal map syntax in Neo4j 2.0 and do something like:
MATCH (n:Person)
RETURN { Name: n.name , Born: n.born } as Person
ORDER BY n.born
LIMIT 5
Related
I'm creating a lexer which creates tokens and outputs them as a JSON list. The tokens are namedtuples.
More specifically, Token = namedtuple('Token', ['kind', 'lexeme'])
I create my tokens and print them using json.dumps(tokens, separators=(',', ':'))).
The output looks like this:
[
[
"INT",
"123"
],
[
"ID"
"b32"
],
]
I am looking to add a 'kind' and 'lexeme' label so that it looks like:
[
[
"kind" : "INT",
"lexeme" : "123"
],
[
"kind" : "ID"
"lexeme" : "b32"
],
]
Any ideas on how to do this?
Convert the namedtuple to dict before running json.dumps().
json.dumps([t._asdict() for t in tokens], separators=(',', ':'))
This should generate:
[
{
"kind" : "INT",
"lexeme" : "123"
},
{
"kind" : "ID",
"lexeme" : "b32"
}
]
Try it online!
I've been working with Firebase, and in a part of my JSON tree, I had a bunch of consecutive numbers as keys.
Here's an abbreviated portion of that part of the tree:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"3" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
When I downloaded it from Firebase and used this:
if let r = ref {
r.child(accessKey).child("matches").observeSingleEvent(of: .value, with: { (snapshot) in
if let groups = snapshot.value as? NSDictionary {
The conversion to NSDictionary failed.
However, when I edited "matches" to look like this:
"matches" : {
"1" : {
"0931-Red" : [
"John Smith"
],
"2022-Blue" : [
"Paul Adams"
]
},
"2" : {
"1489-Red" : [
"Matthew Brown"
],
"1565-Blue" : [
"Ian Fowler"
]
},
"4" : {
"1652-Red" : [
""
],
"2626-Blue" : [
""
]
}
}
It worked.
I was able to convert the data to an NSDictionary.
This also worked by changing the first "1" to "01" and appending a string to the beginning of each number like "match-1", "match-2", etc.
Even though my code works, the error is still bothering me because I don't know why it was wrong in the first place.
Why didn't it work with consecutive numbers as keys in the first place?
Thanks in advance!
I have the JSON that you can see below and I want to sum the values of the two objects, but when I make an aggregation it returns me 0.Here you can see the query that I use; really the first line I only use it to be sure that the path works, and it does. On the other hand,when I use this path in the aggregation query it gives me the "ID" and the "COUNT" with right values,but the "SUM" is always 0 when it must be 3600.Any idea?
db.getCollection('TEST').find({"prices.year.months.day.csv.price.valPrice":1800})
db.TEST.aggregate([
{ $match: {"location.cp":"20830"}},
{$group:{_id:"20830",total:{$sum:"$prices.year.months.day.csv.price.valPrice"}, count: { $sum: 1 }
}}])
And this is the JSON:
{
"_id" : "20830:cas:S:3639",
"lodgtype" : "Casa",
"lodg" : "Motrico: country holiday home - San sebastian",
"webid" : "6107939",
"location" : {
"thcod" : "20",
"cp" : "20830",
"th" : "Gipuzkoa",
"geometry" : {
"type" : "Point",
"coordinates" : [
43.31706238,
-2.40293598
]
}
},
"prices" : {
"year" : [
{
"valYear" : "2018",
"months" : [
{
"valMonth" : "02",
"day" : [
{
"valDay" : "13",
"csv" : [
{
"valCsv" : "20180205210908_223",
"price" : [
{
"valPrice" : 1800.0
}
]
}
]
}
]
}
]
}
]
},
"reg" : {
"created" : "20180213",
"updated" : "20180213",
"viewed" : "20180213"
}
},{
"_id" : "TEST20830:cas:S:3639",
"lodgtype" : "Casa",
"lodg" : "TESTMotrico: country holiday home - San sebastian",
"webid" : "6107930",
"location" : {
"thcod" : "20",
"cp" : "20830",
"th" : "Gipuzkoa",
"geometry" : {
"type" : "Point",
"coordinates" : [
43.31706238,
-2.40293598
]
}
},
"prices" : {
"year" : [
{
"valYear" : "2018",
"months" : [
{
"valMonth" : "02",
"day" : [
{
"valDay" : "13",
"csv" : [
{
"valCsv" : "20180205210908_223",
"price" : [
{
"valPrice" : 1800.0
}
]
}
]
}
]
}
]
}
]
},
"reg" : {
"created" : "20180213",
"updated" : "20180213",
"viewed" : "20180213"
}
}
Since you've deeply nested array you've to unwind to flatten to a document structure. To count the number of matches you've to use extra group after $match with $push with $$ROOT to keep the matching data.
db.TEST.aggregate([
{"$match":{"location.cp":"20830"}},
{"$group":{
"_id":"20830",
"data":{"$push":"$$ROOT"},
"count":{"$sum":1}
}},
{"$unwind":"$data.prices.year"},
{"$unwind":"$data.prices.year"},
{"$unwind":"$data.prices.year.months"},
{"$unwind":"$data.prices.year.months.day"},
{"$unwind":"$data.prices.year.months.day.csv"},
{"$unwind":"$data.prices.year.months.day.csv.price"},
{"$group":{
"_id":"20830",
"total":{"$sum":"$prices.year.months.day.csv.price.valPrice"},
"count":{"$first":"$count"}
}}
])
I am using JSONLint to parse some JSON and i keep getting the error:
Error: Parse error on line 1:
[{“ product”: [{“
---^
Expecting 'STRING', '}', got 'undefined'
This is the code:
[
{
“product” : [ { “code” : “Abc123”, “description” : “Saw blade”, “price” : 34.95 } ],
“vendor” : [ { “name” : “Acme Hardware”, “state” : “New Jersey” } ]
},
{
“product” : [ { “code” : “Def456”, “description” : “Hammer”, “price” : 22.51 } ],
},
{
“product” : [ { “code” : “Ghi789”, “description” : “Wrench”, “price” : 12.15 } ],
“vendor” : [ { “name” : “Acme Hardware”, “state” : “New Jersey” } ]
},
{
“product” : [ { “code” : “Jkl012”, “description” : “Pliers”, “price” : 14.54 } ],
“vendor” : [ { “name” : “Norwegian Tool Suppliers”, “state” : “Kentucky” } ]
}
]
JSON string literals must use normal quote characters ("), not smart quotes (“”).
You're using some unicode double quotes characters. Replace them with the normal " double quotes.
You also had some extra comma at the end in the second element.
Now it's alright
[
{
"product" : [ { "code" : "Abc123", "description" : "Saw blade", "price" : 34.95 } ],
"vendor" : [ { "name" : "Acme Hardware", "state" : "New Jersey" } ]
},
{
"product" : [ { "code" : "Def456", "description" : "Hammer", "price" : 22.51 } ]
},
{
"product" : [ { "code" : "Ghi789", "description" : "Wrench", "price" : 12.15 } ],
"vendor" : [ { "name" : "Acme Hardware", "state" : "New Jersey" } ]
},
{
"product" : [ { "code" : "Jkl012", "description" : "Pliers", "price" : 14.54 } ],
"vendor" : [ { "name" : "Norwegian Tool Suppliers", "state" : "Kentucky" } ]
}
]
JSON must use normal quote characters("), not smart quotes for(“”) for string literals.
To get the normal quote in JSON data format:
right-click on browser window and select - view page source.
This is how I save the MySQL text format and get the json_decode data
[{"5":[29,30,5],"6":[1,2,3],"7":[4,5,6]}]
$row_days= $rows['days'];
var_dump(json_decode($row_days, true));
Result array (size=1)
0 => array (size=3) 5 => array (size=3) 0 => int 29 1 => int 30 2 => int 5 6 => array (size=3) 0 => int 1 1 => int 2 2 => int 3 7 => array (size=3) 0 => int 4 1 => int 5 2 => int 6
Many times this error is caused by doing:
object.age = 31
instead of the correct way:
object["age"] = 31
I have the following sample data in MongoDB:
{
"_id" : ObjectId("54833e93ade1a1521a2a2fe8"),
"fname" : "yumi",
"mname" : "sakura",
"lname" : "kirisaki",
"consultations" : [
{
"medications" : [
"paracetamol",
"ibuprofen",
"carbocisteine"
],
"diagnosis" : [
"sore throat",
"fever",
"cough"
],
"date" : ISODate("2014-12-01T16:00:00Z")
},
{
"medications" : [
"paracetamol",
"carbocisteine",
"afrin"
],
"diagnosis" : [
"cough",
"colds",
"fever"
],
"date" : ISODate("2014-12-11T16:00:00Z")
}
]
}
{
"_id" : ObjectId("54833e93ade1a1521a2a2fe9"),
"fname" : "james",
"mname" : "legaspi",
"lname" : "reyes",
"consultations" : [
{
"medications" : [
"zanamivir",
"ibuprofen",
"paracetamol"
],
"diagnosis" : [
"influenza",
"body aches",
"headache"
],
"date" : ISODate("2014-10-22T16:00:00Z")
},
{
"medications" : [
"carbocisteine",
"albuterol",
"ibuprofen"
],
"diagnosis" : [
"asthma",
"cough",
"headache"
],
"date" : ISODate("2014-11-13T16:00:00Z")
}
]
}
I am trying to query patients with zanamivir AND ibuprofen AND cough:
db.patient.find({
$and:
[
{"consultations.medications":["zanamivir", "ibuprofen"]},
{"consultations.diagnosis":"cough"}
]
}).pretty()
So, in the short sample data, I was hoping james would be returned since he is the only one with zanamivir medication.
Nothing is happening when I enter the above query in cmd. It just goes to the next line (no syntax errors, etc.)
How must I go about the query?
You need the use the $all operator.
db.patient.find({
"consultations.medications": { "$all" : [ "zanamivir", "ibuprofen" ]},
"consultations.diagnosis": "cough"
})
Pretty simple, it's just your first part of the query.
db.patient.find({
$and:[
{"consultations.medications":["zanamivir", "ibuprofen"]},
{"consultations.diagnosis":"cough"}]})
Asking Mongodb to find consultations.medications against ["zanamivir", "ibuprofen"] is asking it to find someone whose medications are equal to ['zanamivir', 'ibuprofen'].
If you want to find people who have had zanamivir and ibuprofen medicated you need to tweak the query to this:
db.patient.find({
$and:[
{"consultations.medications":"zanamivir"},
{"consultations.medications":"ibuprofen"},
{"consultations.diagnosis":"cough"}]})
Enjoy!