0

I have a test shortcode to read a JSON file:

function test( ) {
    $strJsonFileContents = file_get_contents("./node.json");
    $array = json_decode($strJsonFileContents, true);
    echo var_dump($array); // print array
}
add_shortcode( 'test', 'test' );

Adding this shortcode into a post I can't update it due to the error

Updating failed. The response is not a valid JSON response.

Following the article How to Fix The Invalid JSON Error in WordPress (Beginner's Guide), I activate the Health Check & Troubleshooting, use the Classic Editor add the debug code into the wp-config.php :

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );

And this is the result:

Warning: file_get_contents(./node.json): failed to open stream: No such file or directory in /home/xnqucuhr5aza/public_html/wp-content/plugins/Custom 1/Custom plugin.php on line 67
NULL

Why does this happen? The file is in the same directory to the plugin (i.e. /home/xnqucuhr5aza/public_html/wp-content/plugins/Custom 1/node.json). I then try file_get_contents(__DIR__ . '/node.json'); but it just says NULL and nothing else.

Ooker
  • 1,969
  • 4
  • 28
  • 58
  • that looks like a relative path, are you sure it's loading from the same location on every request? Are you sure that the file itself starts with the characters `./`? – Tom J Nowell Jan 11 '22 at 18:03
  • I'm sure the file is always there. Does that answer your question? The file name is node.json. But I also tried the `file_get_contents("node.json")` before trying the relative path. Still not work – Ooker Jan 11 '22 at 18:04
  • 1
    I think you missed what I was asking, the file does not move, but the place it's looking for might, wouldn't it be safer to use an absolute path? e.g. `__DIR__ . '/node.json'`? – Tom J Nowell Jan 11 '22 at 18:05
  • I also have tried the full url (accessible from browser). Doesn't work. If the location of the plugin doesn't change, then why would the place it's looking for change? – Ooker Jan 11 '22 at 18:07
  • 1
    because the URL you're loading changes, I'd also avoid loading a URL, use an absolute file path instead. Also are you sure this belongs on this stack? None of this looks like it's related to WordPress, but general PHP – Tom J Nowell Jan 11 '22 at 18:50
  • @TomJNowell it only says " NULL NULL" and nothing else. I post it here because the initial problem is the "not a valid JSON response" problem, which seems to related to Gutenberg – Ooker Jan 12 '22 at 09:17
  • do you mean the result of `json_decode` is `null` or the `file_get_contents` is `null`? There's no information about the contents of the file in your Q, but it's pretty clear at this point this is a generic PHP problem, it has nothing to do with WP other than circumstance – Tom J Nowell Jan 12 '22 at 10:00
  • @TomJNowell hmm, I add `var_dump($strJsonFileContents);` and it does print the file. So perhaps it's `json_decode` which is null. Thanks for your guidance, please help me migrate this to SO – Ooker Jan 12 '22 at 10:28
  • 1
    @Ooker "perhaps it's `json_decode` which is null" - you could confirm that by doing a `var_dump( json_decode($strJsonFileContents, true) );`. And a friendly reminder, `var_dump()` echo the output, so no need for the `echo` as in `echo var_dump`.. You could also validate your JSON syntax (i.e. the value of `$strJsonFileContents`), e.g. using an online tool like https://jsonlint.com/, and if the syntax is good, then PHP would also parse the data without returning a null or throwing an error ✌ – Sally CJ Jan 13 '22 at 02:19
  • 1
    And the [manual](https://www.php.net/manual/en/function.json-decode.php) actually says, "`null` is returned if the *json cannot be decoded* or if the *encoded data is deeper than the nesting limit*", so check the nesting limit? – Sally CJ Jan 13 '22 at 03:12

1 Answers1

1

The "not a valid JSON response" error is because your shortcode is written incorrectly.

As documented, shortcodes need to return their output. Otherwise it will be printed as soon as WordPress processes the shortcodes, which is before they are output.

In this case it causes the output of your shortcode to be printed at the beginning of an API response in the editor, breaking the formatting of that response.

You need to return the output of the shortcode:

function test( ) {
    ob_start();

    $strJsonFileContents = file_get_contents("./node.json");
    $array = json_decode($strJsonFileContents, true);
    var_dump($array); // print array

    return ob_get_clean();
}
add_shortcode( 'test', 'test' );

Because you're using var_dump(), which only prints, I have used output buffering to capture the output and return it.

  • I see. But even when I use absolute file path (`file_get_contents(__DIR__ . "/node.json")`), it still returns NULL – Ooker Jan 12 '22 at 09:46
  • I don’t know the structure of your project. What’s the path to the file with this code, and what’s the path to the json file. I notice your plugin directory has a space in it. This could be a problem. Regardless, that’s a PHP issue, and not related to WordPress. – Jacob Peattie Jan 12 '22 at 09:57
  • I remove the space and this error persists. The path to the file is now `/home/xnqucuhr5aza/public_html/wp-content/plugins/Custom/Custom plugin.php`, and the path to the json file is now `/home/xnqucuhr5aza/public_html/wp-content/plugins/Custom/node.json`. At least it seems that the plugin has found the json file? – Ooker Jan 12 '22 at 10:23