Table of Contents
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.
- Start a linked table. Go to wpDataTables → Create a Table and click Create a data table linked to an existing data source.
- Name the table. Click New wpDataTable and type a name, for example Star Wars people.
- Choose the source type. In the Data source tab, open Input data source type and choose Nested JSON.
- Paste the URL. Paste the API URL in Input JSON URL. Under Choose method, keep GET, or choose POST if your API expects it.
- Load the roots. Click Get JSON roots. wpDataTables reads the JSON and fills Choose JSON root with all its levels.
- Pick the root. In Choose JSON root, choose the level that holds your rows. For the example API, choose root→results.
- Save the table. Click Save Changes. The table preview shows one row per object.

Video: choosing a JSON root and an authentication option for a nested JSON table.
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. |

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:
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.
Where can I read more?
These pages cover related sources and developer options: