Look up key from Json Object - Neo4j - neo4j

In Neo4j , is there a APOC function that i can use to get value a from jsonObject by passing a Key. For example :
My JsonObject is :
{ "masterName" : {"name1" : "A1" , "name2" : "A2", "name3": "A3", "name4" : "A4"}}
and while importing my csv that has "name" field (values : name1, name2, name3, etc) for which I want to lookup above JsonObjet and get respective value to create nodes.

Assuming that you have your json in a field called myJSON, you could
WITH ‘name1’ AS key, line
WITH apoc.convert.fromJsonMap(line.myJSON)[key] AS name

Related

How to do CosmosDB JOIN to get an array of Column values

I have following data in cosmosDb
[
{
accountId : "12453"
tag : "abc"
},
{
accountId : "12453"
tag : "bcd"
},
{
accountId : "34567"
tag : "qwe"
},
{
accountId : "34567"
tag : "xcx"
}
]
And desired output is something like this
[
{
accountId : "123453"
tag : {
"abc",
"bcd"
},
{
accountId : "34567"
tag : {
"qwe",
"xcx"
}
]
I tried Join, array and group by in multiple ways but didn't work.
Below query gives similar output but rather than count I am looking for an array of tags
select c.accountId, count(c.tag) from c where c.accountId = "12453" group BY c.accountId
Also tried below query
select c.accountId, ARRAY(select c.tag from c IN f where f.accountId = "12453") as tags FROM f
This doesn't return any tag values and I get below output but I need distinct accountId and when I try DISTINCT on accountId, it gives an error.
[
{
accountId : "12,
tag : []
}
........
]
Can someone please help with correct query syntax
As M0 B said, this is not supported. Cosmos DB can't combine arrays across documents. You need to handle this on your client side.

Rails PSQL query JSON for nested array and objects

So I have a json (in text field) and I'm using postgresql and I need to query the field but it's nested a bit deep. Here's the format:
[
{
"name":"First Things",
"items":[
{
"name":"Foo Bar Item 1",
"price":"10.00"
},
{
"name":"Foo Item 2",
"price":"20.00"
}
]
},
{
"name":"Second Things",
"items": [
{
"name":"Bar Item 3",
"price":"15.00"
}
]
}
]
And I need to query the name INSIDE the items node. I have tried some queries but to no avail, like:
.where('this_json::JSON #> [{"items": [{"name": ?}]}]', "%#{name}%"). How should I go about here?
I can query normal JSON format like this_json::JSON -> 'key' = ? but need help with this bit.
Here you need to use json_array_elements() twice, as your top level document contains array of json, than items key has array of sub documents. Sample psql query may be the following:
SELECT
item->>'name' AS item_name,
item->>'price' AS item_price
FROM t,
json_array_elements(t.v) js_val,
json_array_elements(js_val->'items') item;
where t - is the name of your table, v - name of your JSON column.

Storing complex data in redis

I started using redis (with ruby on rails) recently and I want to know what is the best way to store this kind of data.
data1 = {
'name2' : {
'age' : xxx,
'height' : xxx,
},
'name2' : {
'age' : xxx,
'weight' : xxx,
}
}
data2 = {
'class1' : {
'num' : xxx,
'location' : xxx,
'teacher' : xxx,
},
'class2' : {
'num' : xxx,
'location' : xxx,
'teacher' : xxx,
}
}
I have tried using the hash map (hset, hmset, hget, hmget) commands but they dont seem to work with the sub keys like "age" and "height".
It appears that you are trying to store some JSON in Redis. Using the redis-rb gem it's pretty trivial to do this. For example you could do the following:
redis = Redis.new
redis.set("key", data1)
Then when you would like to retrieve this data, I would do something like this:
data = JSON.parse(redis.get("key"))
This will retrieve the JSON object that you have stored in Redis with the key named "key", and then parse that into a Ruby hash. I hope this helps!
In Redis hashes you can't store nested elements directly that's why you get those errors.
If you want to be able to access directly items like data1.name1 or data2.class2 then using hashes is the right thing. And to store them you can put everything inside data1.name1 as a json:
HSET data1 name1 {'num' : xxx,'location' : xxx,'teacher' : xxx,}
and to load the data it would be:
HGET data1 name1
But if you don't want to load these fields directly and you can load all what is inside data1 or data2 then vaughanj answer is what you need.

Neo4j and json creating multiple nodes with multiple params

I tried many things but of no use.
I have already raised a question on stackoverflow earlier but I am still facing the same issue.
Here is the link to old stackoverflow question
creating multiple nodes with properties in json in neo4j
Let me try out explaining with a small example
This is the query I want to execute
{
"params" : {
"props" : [
{
"LocalAsNumber" : 0,
"NodeDescription" : "10TiMOS-B-4.0.R2 ",
"NodeId" : "10.227.28.95",
"NodeName" : "BLR_WAO_SARF7"
}
]
},
"query" : "MATCH (n:Router) where n.NodeId = {props}.NodeId RETURN n"}
For simplicity I have added only 1 props array otherwise there are around 5000 props. Now I want to execute the query above but it fails.
I tried using (props.NodeId}, {props[NodeID]} but everything fails.
Is it possbile to access a individual property in neo4j?
My prog is in c++ and I am using jsoncpp and curl to fire my queries.
If you do {props}.nodeId in the query then the props parameter must be a map, but you pass in an array. Do
"props" : {
"LocalAsNumber" : 0,
"NodeDescription" : "10TiMOS-B-4.0.R2 ",
"NodeId" : "10.227.28.95",
"NodeName" : "BLR_WAO_SARF7"
}
You can use an array of maps for parameter either with a simple CREATE statement.
CREATE ({props})
or if you loop through the array to access the individual maps
FOREACH (prop IN {props} |
MERGE (i:Interface {nodeId:prop.nodeId})
ON CREATE SET i = prop
)
Does this query string work for you?
"MATCH (n:Router) RETURN [p IN {props} WHERE n.NodeId = p.NodeId | n];"

Neo4j Create only if not present query

I am using json to create nodes in noe4j
I have written a small c++ prog to do so using curl and json
Now i have to create around 10000 nodes in neo4j with properties having name and value.
For that i am using props in json with the query as
{
"params" : {
"props" : {
[{name : "a", value : 1}, {name : "b", value : 2}......so on]
]
}
},
"query" : "CREATE (n:Router { props }) RETURN n"
}
the question is I just want to create that nodes with unique names. If a node is already present with the name as in json props I do not want to create it.
How to write a query for these types of request in neo4j
Change your query to the following:
{
"params" : {
"props" : {
[{name : "a", value : 1}, {name : "b", value : 2}......so on]
]
}
},
"query" : "FOREACH (router in {props} | MERGE (n:Router {name: router.name}) ON CREATE SET n = router)"
}
Basically it iterate the items in your list, check for name property if it exist and it case it save a new node

Resources