How to import nested JSON files to Magento 2

- Magento 2, Our extensions

Magento 2 Development; Magento 2 tutorial

By default, Magento 2 doesn’t let you use JSON files to import data to your e-commerce website. While you can fix that with the help of some third-party extensions, they usually fail when it comes to complex JSON structures or nested JSON files. If there is more than one nested level, you won’t be able to import data to Magento 2. However, we know the solution, and it is our Improved Import & Export Magento 2 extension.

Below, you will learn how to import nested JSON files to Magento 2 if the desired information is hidden deep in their structure. In a nutshell, it is necessary to specify a path to the data within the file, but let’s see the details. 

What is nested JSON?

Under nested JSON we assume a JSON file with many of its values being other JSON objects. In other words, it is a file with a complex structure that looks like a category tree of your e-commerce website.

magento 2 store categories

You can see that one category is hidden within another one. So are entities of a nested JSON file.

Nested JSON provides higher clarity than simple JSON because it decouples objects into different layers. Consequently, such files are associated with better maintenance, but they are difficult to understand regarding Magento 2 import. 

What’s wrong with nested JSON files and Magento 2 import? 

With nested JSON, objects can be nested within other objects. Although it enhances usability and maintenance, such files become more complicated to read and understand for various Magento 2 import solutions. Each nested object has a unique access path, and your chosen import instrument may not know how to read it. The same field name can occur in nested objects in the same document making it difficult for modules to fetch the necessary data. However, you can easily address this issue with the help of the Improved Import & Export extension.

How to import nested JSON files to Magento 2?

The Improved Import & Export extension allows importing nested JSON files to Magento 2. You can use the module to work with files of any complexity. Now, when an update file has a specific path in its structure, you just need to specify it. 

Follow these steps to import nested JSON files to Magento 2:

  1. Open the Improved Import & Export extension;
  2. Open a new import job;
  3. Select your import entity;
  4. Configure other job settings;
  5. Proceed to the Import Source section:
    • Choose JSON as your import file type:
    • Enable ‘Set path to the entity structure in the file’.
    • Specify the path to the desired data in your nested JSON file in the ‘Path to the import entity structure’ field.
      Don’t confuse  ‘Path to the import entity structure’  and ‘File path’. While the former is responsible for the path within the file, the latter indicates where your import file is situated.
      import source for nested JSON
  6. Continue configuring your import job and save it. 

Let’s peruse a small example to describe the process in more detail. Below, you can see a JSON file with a complex structure:

Where you deal with

to import “products1”. Specify ‘newProductKey1/newProductKey2/newProductKey3‘ in the ‘Path to the import entity structure’ field to import product information available in the file:

path to the import entity structure field

Congratulations! Now, you can import nested JSON files of any complexity to Magento 2.

In addition to the ability to work with JSON files of any complexity, the Improved Import & Export extension lets you create cron-based schedules to import them automatically. You can also use mapping if these files contain third-party attributes! 

Moreover, JSON is not the only supported format. Improved Import & Export works with CSV, XML, XLSX, etc. It also supports various file sources, such as Google Sheets, Dropbox, Google Drive, etc. And if standard file transfers are not what you’ve been looking for, the module provides the ability to run API-based import and export processes. Follow this link to learn more about the Improved Import & Export extension: 

Get Improved Import & Export Magento 2 Extension

Also, check our Magento 2 Cookbook for more useful tips.