Get data from JSON file in WordPress

When working with JSON format please note: Currently, wpDataTables accepts this, and only this, format of JSON (see example): a one-level array of same-structured objects where each object will be parsed as a row, and each field inside of the object as a cell. If your software does not return this format, you would need to prepare, as an example some PHP adapter to return one of the formats that wpDataTables would accept.

The JSON (JavaScript Object Notation) format was first introduced as a simple way to serialize and stringify objects or arrays for use in JavaScript snippets. JSON eventually proved to be so convenient, that it became wide-spread, and used as a standard for various web services. Many online platforms can return data in this format, and it is easy to generate it manually in most programming languages used in the web. wpDataTables also supports the JSON format as an input data source.

In this example, we will use a sample JSON file with data for 100 imaginary employees. You can download this file for re-use via this link.

For security reasons, WordPress only allows certain file types to be uploaded through the WordPress Admin Dashboard by default. We’ll go over what MIME file types are, which ones WordPress doesn’t restrict, and a few ways to remove the restriction and enable the uploading of JSON files (and any additional types).

Table of Contents

What are MIME File Types?

A Multipurpose Internet Mail Extensions type is a standard way of data type classification used with web technology, such as browsers, to determine how the file will be handled.

A MIME type is made of two parts – a type and subtype, separated by a ‘/’. Some common MIME types are ‘application/json’ , ‘image/png’, and ‘text/html’.

A use-case example is how the browser would determine by a .png file’s MIME type that it must be handled as an image file type, and not as any other file type, such as audio or video.

MIME File Types WordPress Allows by Default

Audio

  • .mp3
  • .m4a
  • .ogg
  • .wa

Documents

  • .pdf
  • .doc, .docx
  • .ppt, .pptx, .pps, .ppsx
  • .odt
  • .xls, .xlsx
  • .psd

Images

  • .jpg
  • .jpeg
  • .png
  • .gif
  • .ico

Videos

  • .mp4
  • .m4v
  • .mov
  • .wmv
  • .avi
  • .mpg
  • .ogv
  • .3gp
  • .3g2

Ways to Upload JSON to WordPress

Below are a few different ways we can tell WordPress to allow JSON uploads:

Allow JSON File Uploads via a Plugin

This is the most straight-forward approach, especially if you don’t want to edit any code.
Here are some recommended plugins that are free and simple to use:

WP Add Mime Types

  1. After downloading the plugin, go to Settings -> MIME Type Settings
  2. In the “Add Values” settings, add: json = application/json
  3. Click the “Save” button
  4. Check that it has been added to the “List of allowed mime types and file extensions by WordPress“
Get data from JSON file in WordPress
Get data from JSON file in WordPress


WP Extra File Types Plugin

WP Media File Type Manager

Edit functions.php or custom plugin file

Using the upload_mimes filter will change the default MIME types list allowed by adding each of your desired file types to it.
In your theme’s function.php file (or custom plugin file), add the following code (lines 7-11):

Note: Usually the MIME type for JSON used here would be ”application/json, but because of a current WordPress core bug it’s being interpreted as ‘text/plain’. Therefore, in the code above we use ‘text/plain’.

Edit wp-config.php

WordPress has an option that can be enabled in the wp-config.php file. This will allow ALL file types to be uploaded by administrator level users, so it may be a bit overkill to enable it if you want just one specific file type enabled.
In the wp-config.php file, add the following code:

define('ALLOW_UNFILTERED_UPLOADS', true);

Summary

MIME type files have the purpose of giving the browser data information so that it knows what to do with media files. WordPress, by default, allows a limited type of files that can be uploaded and that does not include JSON. To set WordPress to accept JSON files for uploading you can either: use a plugin to change the settings, or edit/add code to the functions.php or wp-config.php files.

If you’re using WordPress to host your own content, then you’ll likely want to use JSON for your data files. JSON is a lightweight data format that’s popular with developers and makes it easy to upload JSON files to WordPress and manage your JSON content.

A wealth of knowledge is available on the web about JSON, and it’s a topic that gets some significant attention among developers. Even as I write this article, I get emails from people who’ve heard of JSON but don’t know what it is. And while there are many blog posts, articles, and tutorials on the topic, there is very little that goes into the natty gritty of how to actually format a JSON file for WordPress.

Table of Contents

How to Upload JSON File to WordPress

Uploading JSON files to WordPress can be a daunting task, but with a few simple steps, it’s easy to get your content up and running. In this guide, we’ll show you how to upload JSON files to WordPress site.

A Brief History of JSON

A) It’s not XML, which is another data format. Another reason why other people should avoid using JSON.

B) The json_encode() function can only be used on strings (which makes sense given that they contain numbers).

C)You can store anything in it. The only things that are not legal are arrays and objects (which are still legal), but there are no rules against those either.

D) There’s no defined number of characters for each datatype, so any string with more than 255 characters will not be valid. However, most programs will return a valid string if you type “1” at the command line or pass it as an argument to an application; just make sure you don’t pass it an integer value greater than its length.

E) When creating JSON, you can use one of two forms: new object [array] or new array [object].Or you can even use an empty array directly as a single item in the serialized JSON object, but this is usually best avoided since it results in a fairly useless string (I don’t know why this happens).

F) Many array and object manipulation operators are available, including indexOf(), lastIndexOf(), min(), max(), random(), getItem(), setItem(value), and setValue(value). All these functions accept an optional second parameter which specifies what sort of operation to perform on the first parameter—you can also combine multiple operations into one call if you like—and return either a boolean or a corresponding value depending on what was applied. To find out more about these functions, see here.

G) Other string manipulation functions include split(), join(), splitWith(), joinWith(), length(), trim(), filter(), contains, prepend, substr(), insertNewlineAfter, insertTrimAtEnd, and trimNewlineBefore. This list is far from complete, though I doubt there will ever be any special operators added that aren’t already supported by JavaScript’s built-in operators!

Get data from JSON file in WordPress

Woo Product Table Pro

The Most Popular Product Table Plugin For WooCommerce

Get Special Discount

What is JSON format?

JSON (JavaScript Object Notation) is a data format used for transmitting data in web applications. JSON was designed to be human-readable, which makes it ideal for transferring data between a server and a client. In addition, JSON is lightweight and can be sent over HTTP networks without affecting performance. JSON data is easy to read and write, making it ideal for transmitting data between the server and client.

JSON is a standard text-based format for representing structured data. It is most commonly used in web development, but can be used in any scenario where data needs to be exchanged between programs. JSON was designed as a replacement for XML, which was originally designed as a markup language for describing the structure of documents.

There are two main types of JSON: compact and tall. Compact JSON is shorter and easier to read, while tall JSON is more verbose and allows for more complex structures.

Here is an example of JSON data:

JSON Strings & Numbers

The problem with WordPress JSON strings is that you can’t use them directly in your code. You must use the JavaScript function wp_parse_json() .

To upload JSON files to WordPress, you need to use some special characters, including spaces and colons (“:”). You can also use curly brackets (“{ } ”) as well as arbitrary arrays.

Use the following form when uploading JSON files:
The following form will upload any number of JSON files to WordPress:
You can find more information about this topic on our blog post on WordPress Codex.

Why do you need a json file for WordPress?

It’s easy to read and write, making it perfect for building applications with PHP, MySQL and JavaScript. JSON files are also valid HTML documents, so you can use them to create dynamic websites.

It is efficient for data exchanges between web applications and back-end systems. JSON supports objects, arrays, strings, numbers, and Booleans. It also has a built-in key/value storage mechanism. Because JSON is lightweight and efficient, it can be used to build small applications or as part of a larger application architecture.

If you want to upload a JSON file to your WordPress blog, here’s how to do it:

By Using Plugin:

It is quite impossible to upload JSON files in the right way with no plugin. Because WordPress supports Multipurpose Internet Mail Extensions type, it is a standard way of classifying data.
Here is the most popular plugins list.

  • WP Extra File Types Plugin
  • WP Media File Type Manager
  • (Popular Ones)WP Add Mime Types

These plugins are free and easy to use.

Example:

Here we used WP Add Mime Types. It is one of the most popular WordPress plugin.

Step 1:

First you have to install and activate this plugin.

Get data from JSON file in WordPress
Get data from JSON file in WordPress

Step 2:

After that, you have to go to Settings >> MIME Type Settings.
Here you get the WP Add Mime Types Admin Settings area.

Get data from JSON file in WordPress
Get data from JSON file in WordPress

Step 3:

In the WP Add Mime Types Admin Settings area.
You have to put your JSON code in the Add Values input box.
Follow the screenshot below:

Get data from JSON file in WordPress
Get data from JSON file in WordPress

Don’t forget to click on the Save button to save the data.

After saving the data, you can check out your data from the List of allowed mime types and file extensions by WordPress.

Get data from JSON file in WordPress
Get data from JSON file in WordPress

By Edit functions.php:

You need to put the code in the theme’s function.php. Follow the instructions carefully.

Step 1:

Log in to your WordPress dashboard.
Then go to Appearance >> Themes File Editor.

Step 2:

After that, on the right side of the Theme Files bar,
Scroll down and try to find the functions.php file.
Then click on it.

Get data from JSON file in WordPress
Get data from JSON file in WordPress

Step 3:

In the functions.php section,
add the following code like in the screenshot below:

Get data from JSON file in WordPress
Get data from JSON file in WordPress

You must click the Update File button to save the custom code.

By Edit wp-config.php:

On the basis of some demand, Super Admins can be given the capability to be special cases.

For this code, WordPress has an ALL file types option. and also enable it if you want just one specific file type enabled.

In the wp-config.php file,
add the following code:

Related Post

How to increase Maximum Upload size in cPanel

Final Word

Uploading JSON files to WordPress is a simple process that requires no special software or knowledge. Simply navigate to your WordPress site’s content management system (CMS) and upload the JSON file using the file input box.

Once uploaded, the JSON file will be automatically parsed and converted into required WordPress data structures. If you’re new to JSON, this tutorial may provide you with the necessary information to successfully upload JSON files to WordPress.

How do I get JSON data in WordPress?

Accessing all of your site data via the REST API is as simple as composing a URL. For any WordPress site running at least version 4.7, add the following string to the end of your site's url: /wp-json/wp/v2 (e.g., http://example.com/wp-json/wp/v2 ). Put that URL in your browser, and see what comes up.

How do I get info from a JSON file?

To extract the name and projects properties from the JSON string, use the json_extract function as in the following example. The json_extract function takes the column containing the JSON string, and searches it using a JSONPath -like expression with the dot . notation. JSONPath performs a simple tree traversal.

How do I call a JSON file in WordPress?

Allow JSON File Uploads via a Plugin.
After downloading the plugin, go to Settings -> MIME Type Settings..
In the “Add Values” settings, add: json = application/json..
Click the “Save” button..
Check that it has been added to the “List of allowed mime types and file extensions by WordPress“.

Does JSON work with WordPress?

Inserting JSON Code into WordPress Once your code is ready, you need to insert it into your WordPress post. Luckily with Gutenberg, you just need to create a block called “Custom Code” and then paste your JSON into it anywhere on the page (usually on the bottom).