WordPress custom post meta JSON string won’t decode with json_decode()

I have a problem regarding a JSON string grabbed from WordPress custom post meta.

1) I grab the custom post meta and save it as $json1.

Read More
$json1 = get_post_meta(get_the_ID(), 'netr_locations_json', true);

2) I save the same string as $json2, this time hard coded.

$json2 = '{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}';

3) I echo the strings. They are identical.

echo $json1;
echo $json2;

Result:

{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}

{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}

4) I var_dump the decoded strings. The string from WordPress won’t decode and produces NULL even though it’s the same string. The hard coded string decodes just fine.

var_dump(json_decode($json1));
var_dump(json_decode($json2));

Result:

NULL

object(stdClass)#177 (1) { ["locations"]=> array(1) { [0]=> object(stdClass)#173 (6) { ["number"]=> int(1) ["header"]=> string(15) "This is a title" ["desc"]=> string(22) "This is a description." ["address"]=> string(23) "Infinity Loop Cupertino" ["lat"]=> string(10) "37.3322024" ["lng"]=> string(11) "-122.030755" } } } 

What am I missing? What is wrong with the string from WordPress?


Update:

var_dump($json1) gives this:

string(282) "{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}" 

var_dump($json2) gives this:

string(162) "{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}"

What does the integer after string mean?


Update:
This is the results of the hex_dump():

$json1:

00 000 22 7B 26 71 75 6F 74 3B 6C 6F 63 61 74 69 6F 6E "{" location
10 016 73 26 71 75 6F 74 3B 3A 5B 7B 26 71 75 6F 74 3B s": [{"
20 032 6E 75 6D 62 65 72 26 71 75 6F 74 3B 3A 31 2C 26 number&q uot;:1,& 
30 048 71 75 6F 74 3B 68 65 61 64 65 72 26 71 75 6F 74 quot;hea der" 
40 064 3B 3A 26 71 75 6F 74 3B 54 68 69 73 20 69 73 20 ;:" This is 
50 080 61 20 74 69 74 6C 65 26 71 75 6F 74 3B 2C 26 71 a title& quot;,&q 
60 096 75 6F 74 3B 64 65 73 63 26 71 75 6F 74 3B 3A 26 uot;desc ":& 
70 112 71 75 6F 74 3B 54 68 69 73 20 69 73 20 61 20 64 quot;Thi s is a d 
80 128 65 73 63 72 69 70 74 69 6F 6E 2E 26 71 75 6F 74 escripti on." 
90 144 3B 2C 26 71 75 6F 74 3B 61 64 64 72 65 73 73 26 ;," address& 
A0 160 71 75 6F 74 3B 3A 26 71 75 6F 74 3B 49 6E 66 69 quot;:&q uot;Infi 
B0 176 6E 69 74 79 20 4C 6F 6F 70 20 43 75 70 65 72 74 nity Loo p Cupert 
C0 192 69 6E 6F 26 71 75 6F 74 3B 2C 26 71 75 6F 74 3B ino" ;," 
D0 208 6C 61 74 26 71 75 6F 74 3B 3A 26 71 75 6F 74 3B lat" ;:" 
E0 224 33 37 2E 33 33 32 32 30 32 34 26 71 75 6F 74 3B 37.33220 24" 
F0 240 2C 26 71 75 6F 74 3B 6C 6E 67 26 71 75 6F 74 3B ,"l ng" 
100 256 3A 26 71 75 6F 74 3B 2D 31 32 32 2E 30 33 30 37 :"- 122.0307 
110 272 35 35 26 71 75 6F 74 3B 7D 5D 7D 22 55" }]}"

$json2:

00 000 7B 22 6C 6F 63 61 74 69 6F 6E 73 22 3A 5B 7B 22 {"locati ons":[{" 
10 016 6E 75 6D 62 65 72 22 3A 31 2C 22 68 65 61 64 65 number": 1,"heade 
20 032 72 22 3A 22 54 68 69 73 20 69 73 20 61 20 74 69 r":"This is a ti 
30 048 74 6C 65 22 2C 22 64 65 73 63 22 3A 22 54 68 69 tle","de sc":"Thi 
40 064 73 20 69 73 20 61 20 64 65 73 63 72 69 70 74 69 s is a d escripti 
50 080 6F 6E 2E 22 2C 22 61 64 64 72 65 73 73 22 3A 22 on.","ad dress":" 
60 096 49 6E 66 69 6E 69 74 79 20 4C 6F 6F 70 20 43 75 Infinity Loop Cu 
70 112 70 65 72 74 69 6E 6F 22 2C 22 6C 61 74 22 3A 22 pertino" ,"lat":" 
80 128 33 37 2E 33 33 32 32 30 32 34 22 2C 22 6C 6E 67 37.33220 24","lng 
90 144 22 3A 22 2D 31 32 32 2E 30 33 30 37 35 35 22 7D ":"-122. 030755"} 
A0 160 5D 7D ]} 

So there clearly is a difference.


This is the full code:

$json1 = get_post_meta(get_the_ID(), 'netr_locations_json', true);
$json2 = '{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}';

echo $json1;
echo '<br/><br/>';
echo $json2;
echo '<br/><br/>';

var_dump(json_decode($json1));
echo '<br/><br/>';
var_dump(json_decode($json2));

Thank you for taking a look.

Related posts

Leave a Reply

4 comments

  1. Using echo to compare strings does not really work. I suggest you dump the strings into hex values as well. See here for a hex_dump php routine. With this you will see how those differ, can be handy to track down encoding problems.

    json_decode only works with UTF-8 encoded data. I assume the data you pass to the string would not validate UTF-8 and therefore no values can be extraced.

    You must first properly encode the data as UTF-8 before you can use it. To do so you must know about the encoding of the data.

    To validate if a string could be valid UTF-8 data, I’ve posted some code in the question SimpleXML and Chinese. It’s a function named can_be_valid_utf8_statemachine().

    Next to probably encoding issues, when you compare those two values, take care when you display the output in the browser as well. Here is some more “browser compatible” output:

    echo '<pre>', htmlspecialchars($json1), '</pre>';
    echo '<pre>', htmlspecialchars($json2), '</pre>';
    

    If you do this with your strings it should show you where the json got broken according to the hex_dump() output you’ve added to your question.

  2. if stripslashes doesn’t work for you, try str_replace('','',$meta_value)

    eg: json_decode(str_replace('','',get_post_meta( $post->ID, 'google_maps_files', true)),true); worked for me!