Title: wp_json_file_decode
Published: February 3, 2022
Last modified: February 24, 2026

---

# wp_json_file_decode( string $filename, array $options = array() ): mixed

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#wp--skip-link--target)

Reads and decodes a JSON file.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#parameters)󠁿

 `$filename`stringrequired

Path to the JSON file.

`$options`arrayoptional

Options to be used with `json_decode()`.

 * `associative` bool
 * Optional. When `true`, JSON objects will be returned as associative arrays.
    
   When `false`, JSON objects will be returned as objects. Default false.

Default:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#return)󠁿

 mixed Returns the value encoded in JSON in appropriate PHP type.
 `null` is returned
if the file is not found, or its content can’t be decoded.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#source)󠁿

    ```php
    function wp_json_file_decode( $filename, $options = array() ) {
    	$result   = null;
    	$filename = wp_normalize_path( realpath( $filename ) );

    	if ( ! $filename ) {
    		wp_trigger_error(
    			__FUNCTION__,
    			sprintf(
    				/* translators: %s: Path to the JSON file. */
    				__( "File %s doesn't exist!" ),
    				$filename
    			)
    		);
    		return $result;
    	}

    	$options      = wp_parse_args( $options, array( 'associative' => false ) );
    	$decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] );

    	if ( JSON_ERROR_NONE !== json_last_error() ) {
    		wp_trigger_error(
    			__FUNCTION__,
    			sprintf(
    				/* translators: 1: Path to the JSON file, 2: Error message. */
    				__( 'Error when decoding a JSON file at path %1$s: %2$s' ),
    				$filename,
    				json_last_error_msg()
    			)
    		);
    		return $result;
    	}

    	return $decoded_file;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L4664)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L4664-L4697)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_trigger_error()](https://developer.wordpress.org/reference/functions/wp_trigger_error/)`wp-includes/functions.php` |

Generates a user-level error/warning/notice/deprecation message.

  | 
| [wp_normalize_path()](https://developer.wordpress.org/reference/functions/wp_normalize_path/)`wp-includes/functions.php` |

Normalizes a filesystem path.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [wp_parse_args()](https://developer.wordpress.org/reference/functions/wp_parse_args/)`wp-includes/functions.php` |

Merges user defined arguments into defaults array.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#)

| Used by | Description | 
| [WP_Font_Collection::load_from_file()](https://developer.wordpress.org/reference/classes/wp_font_collection/load_from_file/)`wp-includes/fonts/class-wp-font-collection.php` |

Loads the font collection data from a JSON file path.

  | 
| [get_block_metadata_i18n_schema()](https://developer.wordpress.org/reference/functions/get_block_metadata_i18n_schema/)`wp-includes/blocks.php` |

Gets i18n schema for block’s metadata read from `block.json` file.

  | 
| [WP_Theme_JSON_Resolver::read_json_file()](https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/read_json_file/)`wp-includes/class-wp-theme-json-resolver.php` |

Processes a file that adheres to the theme.json schema and returns an array with its contents, or a void array if none found.

  | 
| [WP_Theme_JSON_Resolver::translate()](https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/translate/)`wp-includes/class-wp-theme-json-resolver.php` |

Given a theme.json structure modifies it in place to update certain values by its translated strings according to the language set by the user.

  | 
| [register_block_type_from_metadata()](https://developer.wordpress.org/reference/functions/register_block_type_from_metadata/)`wp-includes/blocks.php` |

Registers a block type from the metadata stored in the `block.json` file.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_json_file_decode/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_json_file_decode%2F)
before being able to contribute a note or feedback.