You are here

Consuming JSON feed using Feeds and JSONPath Parser

One of the best aspects about Drupal is its interoperability. Drupal can be integrated with almost all systems(CMS/ECM/DM)out there. There are so many ways to import content from other systems to Drupal. How you implement it will depend on some factors(budget, time constraint, 3rd party system constraints). If you need to consume a JSON feed and convert them into nodes, try using feeds module in conjunction with feeds_jsonpath_parser module. Use feeds to consume data from a given URL and feeds_jsonpath_parser to traverse through the JSON data and map elements to CCK fields. JSONPath allows you to traverse JSON string like XPATH is used to traverse XML contents. Here is an sample use case that we will solve using Feeds/JSONPath Parser: We need to extract the list of employee information from a given system. We have created an employee content type that will encapsulate employee information that we will consume from a 3rd party system. The employee content type will contain the following fields:

  • Last name (field_employee_lname)
  • First name (field_employee_fname)
  • Employee ID (field_employee_emp_id)
  • Position (field_employee_position)

Here is the sample JSON string that we will consume:

{"companies":{"company_a":{"employees":[{"position":"Developer","first_name":"Bob","last_name":"Williams","employee_id":1},{"position":"Developer","first_name":"Dave","last_name":"Ali","employee_id":2},{"position":"Developer","first_name":"Jon","last_name":"Davis","employee_id":3}]}}}

Here is the list of modules used:

  • feeds (and its dependencies)
  • feeds_import
  • feeds_ui
  • feeds_json
  • path_parser

Here are the steps in order to consume that JSON feed:

1) Navigate to admin/build/feeds/create

2) Enter importer name and description.

3) Click create. NOTE: You will be redirected to the importer configuration page.

4) Modify the following basic settings and click save: - Attach to content type setting: Feed Item - Minimum refresh period: 1 day

5) Make sure the Fetcher is set to HTTP Fetcher.

6) Change Parser to JSONPath parser.

7) Change Processor to Node processor.

8) Modify the following node processor settings and click save. - Content Type: employee - Author: admin - Authorize: checked - Expire: never - Update existing nodes: replace existing nodes

9) Map the following JSONPath expressions with the appropriate employee content type fields.

10) Modify the JSONPath parser settings and click save: - Context: $.companies.company_a.employees[*] - title(node title): last_name - field_employee_last_name: last_name - field_employee_first_name: first_name - field_employee_emp_id: employee_id - guid: employee_id NOTE: Modifying JSONPath settings tells Feeds how to traverse the JSON string and retrieve specific data that Drupal needs to build an employee content.

11) Navigate to admin/build/feeds

12) Create a feed item node that will be responsible for fetching the JSON content by clicking Feed Item (under “Attached to”). 1

3) Enter node title and feed URL.

NOTE: When cron runs, feeds will look for the feed item node that you created and will consume the JSON content from the specified URL. You can also manually consume the JSON content by navigating to the node page of the feed item, clicking the import tab and then clicking import.

SHARE SOCIAL
Developer
Rexx joined Appnovation Developer Team in 2008. He is a clever guy who loves to solve business problems in the most simple and elegant way possible. He is driven and does whatever it takes to get the job done without sacrificing the quality of work.Bucket List Accomplishments: Spring Break in...→ More about Rexx
Developer

Rexx joined Appnovation Developer Team in 2008. He is a clever guy who loves to solve business problems in the most simple and elegant way possible. He is driven and does whatever it takes to get the job done without sacrificing the quality of work.

Bucket List Accomplishments: Spring Break in Cancun! We’ll save the story telling to Rexx.

Weekends are Spent: Playing basketball or at Gary Point and reading Manga...he says its geeky, we say classic!

Favourite Books: 1984 and The Metamorphosis

Appnovation Technologies