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:
Here is the list of modules used:
- feeds (and its dependencies)
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.