Importing JSON Data from a REST API into Neo4j

We are interested in importing the activities for the athlete who is logged in. That endpoint takes the following parameters:

We’re interested in per_page (where we can define the number of activities returned per call to the endpoint) and after (where we can tell the API to only return results after a provided epoch timestamp).

Let’s imagine that we have more activities that we can return in one request to the API. We’ll need to paginate to retrieve all our activities and import them into Neo4j.

Before we paginate the API, let’s first learn how to import one page worth of activities into Neo4j. The following query will return activities starting from the earliest timestamp:

WITH after AS 0
WITH 'https://www.strava.com/api/v3/athlete/activities?after=' + after AS uri
CALL apoc.load.jsonParams(uri, {Authorization: $stravaToken}, null)
YIELD value
CREATE (run:Run {id: value.id})
SET run.distance = toFloat(value.distance),
    run.startDate = datetime(value.start_date_local),
    run.elapsedTime = duration({seconds: value.elapsed_time})

We create a node with the label Run for each activity and set a few properties, as well. The most interesting one for this example is startDate which we will pass to the after parameter later on.

This query will load the first 30 activities, but what if we want to get the next 30? We can change the first line of the query to find the most recent timestamp of any of our Run nodes and then pass that to the API. If there aren’t any Run nodes, then we can use a value of 0 like in the query below.

OPTIONAL MATCH (run:Run)
WITH run ORDER BY run.startDate DESC LIMIT 1
WITH coalesce(run.startDate.epochSeconds, 0) AS after
WITH 'https://www.strava.com/api/v3/athlete/activities?after=' + after AS uri
CALL apoc.load.jsonParams(uri, {Authorization: $stravaToken}, null)
YIELD value
CREATE (run:Run {id: value.id})
SET run.distance = toFloat(value.distance),
    run.startDate = datetime(value.start_date_local),
    run.elapsedTime = duration({seconds: value.elapsed_time})

We could continue to run this query manually, but it’s about time that we automated it.


This is a companion discussion topic for the original entry at https://neo4j.com/developer/guide-import-json-rest-api/