Our Biggest Sale Is Here
Up to 70% Off
Get the Deal

How to create a table from nested JSON with authentication in wpDataTables

Say you want to show the people from a public API in a table, but the API wraps the rows in an object, under a key such as results, next to paging details. Or your CRM’s API only answers with a password or an API key. The Nested JSON source type handles both: you pick the level of the JSON that holds the rows, and you add the login or the headers the API needs.

When do I need Nested JSON instead of a JSON file?

Use Nested JSON when the rows are not the top level of the response, or when the URL needs authentication. For a plain array of objects at the top level, the simpler JSON file source type is enough.

Your data Source type
A top-level array of objects, open URL JSON file
Rows inside an object, such as {"count": 82, "results": [ … ]} Nested JSON
An API that needs a username and password Nested JSON with basic authentication
An API that needs an API key or other headers Nested JSON with custom headers

Your source also needs at least a few rows, and the level you pick must hold objects with the same keys. You can pick only one level per table. We use the free Star Wars API at https://swapi.dev/api/people/ as the example.

How do I create a table from nested JSON?

Create a linked table with Nested JSON as the source type, load the JSON roots, pick the one with your rows and click Save Changes.

  1. Start a linked table. Go to wpDataTables → Create a Table and click Create a data table linked to an existing data source.
  2. Name the table. Click New wpDataTable and type a name, for example Star Wars people.
  3. Choose the source type. In the Data source tab, open Input data source type and choose Nested JSON.
  4. Paste the URL. Paste the API URL in Input JSON URL. Under Choose method, keep GET, or choose POST if your API expects it.
  5. Load the roots. Click Get JSON roots. wpDataTables reads the JSON and fills Choose JSON root with all its levels.
  6. Pick the root. In Choose JSON root, choose the level that holds your rows. For the example API, choose root→results.
  7. Save the table. Click Save Changes. The table preview shows one row per object.
Nested JSON source settings in wpDataTables with the Input JSON URL, the Get JSON roots button and the open list of JSON roots
Picking the JSON root: (1) Input data source type, (2) Input JSON URL, (3) Get JSON roots, (4) the list of roots with root→results.

Video: choosing a JSON root and an authentication option for a nested JSON table.

To change the root later, click Get JSON roots again to load the list. Keys whose values are arrays or objects, such as films in the example, are left out of the table. To show them, use the hooks below.

wpDataTables sanitizes special characters in the URL. If your URL contains one, use its encoded form instead, for example %3E for >.

How do I connect to an API that needs authentication?

Set JSON authentication to Basic Authentication for a username and password, or add Custom headers for an API key or token, before you click Get JSON roots. JSON authentication is No Auth by default.

Setting When to use it What to enter
JSON authentication: Basic Authentication The API asks for a username and password. The username and password of the API user in Basic Authentication Credentials.
Custom headers The API needs an API key, a token or other headers. A key name and a key value per header, for example Authorization and Bearer your-token. Click + Add Row for more headers. Fill in both fields of every pair.
Nested JSON source settings with Choose method, JSON authentication set to Basic Authentication, the credentials, a custom header and Choose JSON root
Authentication for a Nested JSON source: (1) Choose method, (2) JSON authentication, (3) Basic Authentication Credentials, (4) a custom header, (5) + Add Row, (6) Choose JSON root.

Use an API user or key with read access only. The table shows whatever the API returns, so check that the response holds only data your visitors may see.

How do I show arrays from one level deeper?

Turn on the wpdatatables_get_one_level_deep_json_data_from_array_as_string filter for your table, and wpDataTables shows the values of arrays one level deeper in a cell. Add the code to your theme’s functions.php or a code snippets plugin:

/**
 * Parse one level deep in a nested JSON structure.
 *
 * @param bool   $deepParse False by default.
 * @param string $jsonURL   The nested JSON URL.
 * @param int    $tableID   The table ID.
 * @return bool
 */
function parseNestedJSON( $deepParse, $jsonURL, $tableID ) {
    // Only for the table with this JSON URL
    if ( $jsonURL == 'https://swapi.dev/api/people/' ) {
        return true;
    }
    return $deepParse;
}
add_filter( 'wpdatatables_get_one_level_deep_json_data_from_array_as_string', 'parseNestedJSON', 10, 3 );

By default, the values are separated by a line break (an HTML line break tag), so each value shows on its own line in the cell. To use another separator, such as a comma, use the wpdatatables_set_one_level_deep_json_data_separator filter:

/**
 * Change the separator of array values.
 *
 * @param string $arrayValuesSeparator A br tag by default.
 * @param string $jsonURL              The nested JSON URL.
 * @param int    $tableID              The table ID.
 * @return string
 */
function changeArraySeparator( $arrayValuesSeparator, $jsonURL, $tableID ) {
    if ( $jsonURL == 'https://swapi.dev/api/people/' ) {
        return ', ';
    }
    return $arrayValuesSeparator;
}
add_filter( 'wpdatatables_set_one_level_deep_json_data_separator', 'changeArraySeparator', 10, 3 );

What are the limits of nested JSON tables?

Nested JSON tables load all rows into the browser, like every table linked to a data source, so sorting, filtering and paging run in the visitor’s browser:

  • Above about 2,000 to 3,000 rows, pages load slowly. Turn on caching for large sources.
  • Don’t change the keys of the chosen root after you create the table, or the table stops working.
  • You can’t edit the rows in WordPress.

What should I remember about nested JSON tables?

Choose Nested JSON, paste the URL in Input JSON URL, add basic authentication or custom headers if the API needs them, click Get JSON roots and pick the level with your rows. Arrays inside the rows are left out unless you turn on the one-level-deep filter. Encode special characters in the URL, and cache large sources.

Wait, Before You Download!

To get your hands on wpDataTables Lite, please enter your email address below. We’ll send you a direct download link and keep you updated on existing features along with helpful tips and tricks!

By continuing, I accept Privacy Policy and T&C
Mail Box