# Iterate through \[property\] maps, map keys and map\[key\] values

**URL:** https://community.neo4j.com/t/iterate-through-property-maps-map-keys-and-map-key-values/1071
**Category:** Cypher
**Tags:** apoc, cypher
**Created:** [September 5, 2018, 10:16am UTC](https://community.neo4j.com/t/iterate-through-property-maps-map-keys-and-map-key-values/1071 "2018-09-05T10:16:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)
#### Post date: [September 5, 2018, 10:16am UTC](https://community.neo4j.com/t/iterate-through-property-maps-map-keys-and-map-key-values/1071/1 "2018-09-05T10:16:06Z")

</div>

I want to be able to create graphs mimicking sql normal forms (linking parent to child entities through foreigh keys), and i don't want to do it column by column.  
Hence, i need to be able to create relationships between identically named properties, having identical values. Direction of the relationship to be considered later.  
Problem is that i don't know how to access/iterate through "keys" inside a property map (currently using apoc.any.properties(n) to extract all attributes of a node).

So, my query would look like:  
considering two node tags "parent" and "child"  
for each "parent" node p  
for each "child" node c  
for each property of p  
for each property of c  
if **c.property name ~= p.property name** and if c[property name].value=p[property name].value  
then create relationship between p and c

Of course, looks like a horrendous Cartesian product, but the thing i am interested in is marked in bold : how can i iterate through the keys of a map?

---

<div class="post-metadata">

### Author: ![Benoit](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/benoit/32/551_2.png) [@Benoit](https://community.neo4j.com/u/Benoit)
#### Post date: [September 5, 2018, 10:27am UTC](https://community.neo4j.com/t/iterate-through-property-maps-map-keys-and-map-key-values/1071/2 "2018-09-05T10:27:14Z")

</div>

If you want to iterate over the properties of a node, you can use something like that :

```
MATCH (n) WITH n LIMIT 1
UNWIND keys(n) AS key
RETURN n[key]
```

---

<div class="post-metadata">

### Author: ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)
#### Post date: [September 5, 2018, 11:27am UTC](https://community.neo4j.com/t/iterate-through-property-maps-map-keys-and-map-key-values/1071/3 "2018-09-05T11:27:00Z")

</div>

Not sure if you want one rel per property or one in general. Here is the code for the latter.

```
MATCH (p:Parent), (c:Child)
WHERE any(k in keys(p) WHERE p[k] = c[k])
CREATE (p)-[:PARENT_OF]->(c)
```

---

<div class="post-metadata">

### Author: ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)
#### Post date: [September 5, 2018, 12:30pm UTC](https://community.neo4j.com/t/iterate-through-property-maps-map-keys-and-map-key-values/1071/4 "2018-09-05T12:30:54Z")

</div>

Many thanks, both of you. Below works for me:

MATCH (n),(p) where n\<\>p WITH n,p  
UNWIND keys(n) AS keyn  
UNWIND keys(p) as keyp  
with n,p,keyp,keyn WHERE keyp=keyn  
RETURN keyn,n[keyn],keyp,n[keyp] limit 10

I will stick to the UNWIND as not always the key names are the same (hence the ~=)

PS: is there any notepad++ formatter for Cypher? Or, what are you using to format Cypher code?
