Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to Import with Feeds using JSONPath

Parent Feed: 

In order to import some json data, you first need theses modules installed and enabled.

First to import content from another site, you need to create the content type so the data can be saved on your website. The fields from the JSON source should be the same as your content type on your site depending your needs. for exemple if the JSON data array looks like this:

{
 "name": “Johny John”,
 “date_joined”: 02/12/1988
 "title": “Teacher",
 "department": “Astrophysics",
 "email": [email protected],
}

The fields in your content type should look something like this:

  • name - Text field
  • date_joined - Date field
  • title: - Text field
  • department - Text field
  • email - Email field

Depending on where you download the data, there could be just a few or hundreds and thousands of arrays. There is also a great tool to get a view of your JSON file, its called "REST Console” for chrome. (There are similar apps for all other browsers). REST Console will help you fetch the JSON data and display it on front of you.(See image below) This is valuable get to the data field names and context for configuring the JSON Feeds Import.

Rest Console

Now that the Content Type is created, configuring the Feeds Importers is all thats left.

The first configuration page in feeds in the Basic settings.

  • Name & description : Self-explanatory, simply the name of what you are importing.
  • Attach to content type:
  • If standalone form is selected, use this link: http://sitename.com/import to import data.
  • If a content type is selected, you must create a node of that content type to import data.
  • Periodic Import: Run the import on regular basis, by using cron.

The Second is the Fetcher settings.

“Where should I get the data from?” is what the fetcher is saying. In the Fetcher page there is a menu of selection that you can choose from depending your demand. the two most common are “File Upload” or “HTTP Fetcher”(URL).

The Third is the Parser settings.

“How should i read and interpret this data?” is what the parser is saying. This part is important because its where you say to the website that its going to be a JSONPath parser. (see image below)

Now before going in and configuring the JSONPath parser settings, you first need to create the feed mapping.

For the source always select jsonpath_parser. The numbers will increment automatically like in the picture below. Do this for all your content type fields.

Once the mapping is done. Go to the JSONPath parser settings. You should see something like the picture below.

In this picture above you should see all the content type fields in order just like in the mapping page. In the text box you need enter the JSONPath field by referencing the JSON key like in the first picture. As for the context text box this variable depends on how your JSONPath output is constructed. For this example its pretty simple, there is only one context so you put

 “ $.[] “ 

Here is a page where you could find more info about the Context : https://www.drupal.org/node/1059152 .

Finally you should be ready to import JSONPath Feeds. Go to the import page and add the import file or add the feed URL just like below.

Once the import is done you should see your new nodes created in your content page.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web