cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Creating 'Country' label nodes?

Roh
Node Link

Hi

How can I set and create 'Country' nodes?
Trying to create new 'Country' nodes from JSON however the compiler is reading the command 'count' and not letting me bind the string 'Country'. Image below.

If anyone has ideas on how to best set up relationships between official datasets - ie in this case I'm using ISO country codes, would love to know your ideas or best practices.
Thanks!

1 ACCEPTED SOLUTION

clem
Graph Steward

I downloaded the country-codes.json file that you mentioned.

I think there is something wrong with your JSON file. It seems different than the one you pointed me at. In particular, I can't find any strings like
official_name_fr or UNTERM in the the file I downloaded.

I modified my settings (the second makes it more convenient)

apoc.import.file.enabled=true
dbms.directories.import=ABSOLUTE_IMPORT_DIRECTORY

I ran:

call apoc.load.json("file:///country-codes.json") 
yield value as countries

results in:

{
  "continent": "Asia",
  "capital": "Kabul",
  "e164": "93",
  "languages": "Afghan Persian or Dari (official) 50%, Pashto (official) 35%, Turkic languages (primarily Uzbek and Turkmen) 11%, 30 minor languages (primarily Balochi and Pashai) 4%, much bilingualism, but Dari functions as the lingua franca",
  "internet-hosts": "223",
  "geonameid": "1149361",
  "phones-landline": "13500",
  "internet-users": "1000000",
  "country-name": "Afghanistan",
  "fips": "AF",
  "phones-mobile": "18000000",
  "phone-code": "93",
  "area-km2": "647500",
  "gdp": "20650000000",
  "time-zone-in-capital": "Asia/Kabul",
  "iso-numeric": "004",
  "top-level-domain": "af",
  "currency": "Afghani",
  "iso2": "AF",
  "language-codes": "fa-AF,ps,uz-AF,tk",
  "iso3": "AFG"
}

...

The beginning of the JSON that I have is:

[
  {
    "country-name": "Afghanistan",
    "iso2": "AF",
    "iso3": "AFG",
    "top-level-domain": "af",
    "fips": "AF",
    "iso-numeric": "004",
    "geonameid": "1149361",
    "e164": "93",
    "phone-code": "93",
    "continent": "Asia",
    "capital": "Kabul",
    "time-zone-in-capital": "Asia\/Kabul",
    "currency": "Afghani",
    "language-codes": "fa-AF,ps,uz-AF,tk",
    "languages": "Afghan Persian or Dari (official) 50%, Pashto (official) 35%, Turkic languages (primarily Uzbek and Turkmen) 11%, 30 minor languages (primarily Balochi and Pashai) 4%, much bilingualism, but Dari functions as the lingua franca",
    "area-km2": "647500",
    "internet-hosts": "223",
    "internet-users": "1000000",
    "phones-mobile": "18000000",
    "phones-landline": "13500",
    "gdp": "20650000000"
  },

...

Now, that was the first part: loading the JSON file. The next step is to create nodes from the JSON.

The conceptual leap you need to make is to realize the JSON is an array of dictionaries (if you're a python programmer) or maps (if you're a Java/Neo4J programmer). When you have an array in Neo4J, you probably need an UNWIND statement. This is because Cypher is a declarative language, so the statements operate on a group of values. (It's a bit strange if you're used to procedural/functional programming.)

So the entire query will be:

call apoc.load.json("file:///country-codes.json") 
YIELD value AS countries // value is an array of maps containing country data
UNWIND countries AS countryData // UNWIND creates individual maps containing the country data
CREATE(c:Country) // create the node
SET c = countryData  // assign the map data to node c.
RETURN c

Unfortunately, the documentation for apoc.load.json() is intimidating... (I might try improving it.)

This is another view of JSON and node creation (but without the import):

View solution in original post

3 REPLIES 3

clem
Graph Steward

Can you post some sample JSON data? It looks to me that something isn't quite right but it's hard to say what.

Sure, the data is the ISO 3166 country codes, eg https://gist.github.com/emnsen/a2364b401d1cb02ac09a850a57017994

clem
Graph Steward

I downloaded the country-codes.json file that you mentioned.

I think there is something wrong with your JSON file. It seems different than the one you pointed me at. In particular, I can't find any strings like
official_name_fr or UNTERM in the the file I downloaded.

I modified my settings (the second makes it more convenient)

apoc.import.file.enabled=true
dbms.directories.import=ABSOLUTE_IMPORT_DIRECTORY

I ran:

call apoc.load.json("file:///country-codes.json") 
yield value as countries

results in:

{
  "continent": "Asia",
  "capital": "Kabul",
  "e164": "93",
  "languages": "Afghan Persian or Dari (official) 50%, Pashto (official) 35%, Turkic languages (primarily Uzbek and Turkmen) 11%, 30 minor languages (primarily Balochi and Pashai) 4%, much bilingualism, but Dari functions as the lingua franca",
  "internet-hosts": "223",
  "geonameid": "1149361",
  "phones-landline": "13500",
  "internet-users": "1000000",
  "country-name": "Afghanistan",
  "fips": "AF",
  "phones-mobile": "18000000",
  "phone-code": "93",
  "area-km2": "647500",
  "gdp": "20650000000",
  "time-zone-in-capital": "Asia/Kabul",
  "iso-numeric": "004",
  "top-level-domain": "af",
  "currency": "Afghani",
  "iso2": "AF",
  "language-codes": "fa-AF,ps,uz-AF,tk",
  "iso3": "AFG"
}

...

The beginning of the JSON that I have is:

[
  {
    "country-name": "Afghanistan",
    "iso2": "AF",
    "iso3": "AFG",
    "top-level-domain": "af",
    "fips": "AF",
    "iso-numeric": "004",
    "geonameid": "1149361",
    "e164": "93",
    "phone-code": "93",
    "continent": "Asia",
    "capital": "Kabul",
    "time-zone-in-capital": "Asia\/Kabul",
    "currency": "Afghani",
    "language-codes": "fa-AF,ps,uz-AF,tk",
    "languages": "Afghan Persian or Dari (official) 50%, Pashto (official) 35%, Turkic languages (primarily Uzbek and Turkmen) 11%, 30 minor languages (primarily Balochi and Pashai) 4%, much bilingualism, but Dari functions as the lingua franca",
    "area-km2": "647500",
    "internet-hosts": "223",
    "internet-users": "1000000",
    "phones-mobile": "18000000",
    "phones-landline": "13500",
    "gdp": "20650000000"
  },

...

Now, that was the first part: loading the JSON file. The next step is to create nodes from the JSON.

The conceptual leap you need to make is to realize the JSON is an array of dictionaries (if you're a python programmer) or maps (if you're a Java/Neo4J programmer). When you have an array in Neo4J, you probably need an UNWIND statement. This is because Cypher is a declarative language, so the statements operate on a group of values. (It's a bit strange if you're used to procedural/functional programming.)

So the entire query will be:

call apoc.load.json("file:///country-codes.json") 
YIELD value AS countries // value is an array of maps containing country data
UNWIND countries AS countryData // UNWIND creates individual maps containing the country data
CREATE(c:Country) // create the node
SET c = countryData  // assign the map data to node c.
RETURN c

Unfortunately, the documentation for apoc.load.json() is intimidating... (I might try improving it.)

This is another view of JSON and node creation (but without the import):