I'm trying to convert a json string back into an array in wordpress. Here is the json string that I'm getting back from the database:
[
{
"item_title": "Kindle Fire HD 7\", Dolby Audio, Dual-Band Wi-Fi, 16 GB - Includes Special Offers",
"item_url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=2342",
"large_image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/29_51EN7cswSYL.jpg",
"formatted_price": "$199.00",
"stars_url": "http://my.dev/tester/wordpress/wp-content/plugins/ecom/img/stars-4-0.gif",
"product_review": "World\'s most advanced 7\" tablet with stunning HD display, exclusive Dolby audio, and the fastest Wi-Fi found on a tablet. When it comes to HD displays, great resolution is just the start. Kindle Fire HD delivers rich color and deep contrast from every angle, with an advanced polarizing filter and custom anti-glare technology. Our exclusive Dolby Digital Plus audio includes technology to adjust volume, create virtual surround sound, and deliver easier-to-understand dialogue in movies and TV shows.",
"review_rating": "4 out of 5",
"reviews_url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=2347",
"reviews_label": "Read all 9,972 customer reviews",
"create_review_url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=2346",
"create_review_label": "Write a review",
"form_id": "6653b8329db2abc61eba9f32c26d1c4c",
"add_to_cart_action": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/add_to_cart.php",
"asin": "B0083PWAPW",
"total_items": "",
"item_status": "In Stock",
"customer_images": [
"http://my.dev/tester/wordpress/wp-content/uploads/2013/03/29_61hMQEAJpRL.jpg"
],
"disclaimer": "",
"item_attributes": [
{
"attr": "Binding",
"value": "Electronics"
},
{
"attr": "Brand",
"value": "Amazon Digital Services Inc."
},
{
"attr": "Color",
"value": "Black"
},
{
"attr": "EAN",
"value": "2609000005244"
},
{
"attr": "Label",
"value": "Amazon Digital Services, Inc"
},
{
"attr": "Manufacturer",
"value": "Amazon Digital Services, Inc"
},
{
"attr": "Model",
"value": "53-000406"
},
{
"attr": "MPN",
"value": "0379"
},
{
"attr": "PackageQuantity",
"value": "1"
},
{
"attr": "PartNumber",
"value": "0379"
},
{
"attr": "ProductGroup",
"value": "Amazon Devices"
},
{
"attr": "ProductTypeName",
"value": "ABIS_ELECTRONICS"
},
{
"attr": "Publisher",
"value": "Amazon Digital Services, Inc"
},
{
"attr": "ReleaseDate",
"value": "2012-09-14"
},
{
"attr": "Studio",
"value": "Amazon Digital Services, Inc"
},
{
"attr": "Title",
"value": "Kindle Fire HD 7\", Dolby Audio, Dual-Band Wi-Fi, 16 GB - Includes Special Offers"
},
{
"attr": "UPC",
"value": "848719003796"
}
],
"customer_review_url": "http://my.dev/tester/wordpress/wp-content/ecom-customer-reviews/61b553856a18b530399c2158ebbfc71d.html",
"flickr_results": [
"http://my.dev/tester/wordpress/wp-content/uploads/2013/03/29_8418129172_d9cd0358ce_m.jpg"
],
"freebase_text": "Amazon.com, Inc. (NASDAQ:u00a0AMZN) is an American multinational electronic commerce company with headquarters in Seattle, Washington, United States. It is the world\'s largest online retailer. The company also produces consumer electronics - notably the Amazon Kindle e-book reader - and is a major provider of cloud computing services.\\nAmazon has separate retail websites for the following countries: United States, Canada, United Kingdom, France, Germany, Italy, Spain, Japan, and China, with international shipping to certain other countries for some of its products. It is also expected to launch its websites in Poland, Netherlands and Sweden.\\nJeff Bezos incorporated the company (as Cadabra) in July 1994, and the site went online as amazon.com in 1995. The company was renamed after the Amazon River, one of the largest rivers in the world, which in turn was named after Amazons, the legendary nation of female warriors in Greek mythology. Amazon.com started as an online bookstore, but soon diversified, selling DVDs, CDs, MP3 downloads, software, video games, electronics, apparel, furniture, food, toys, and jewelry.\\nThe company was founded in 1994, spurred by what Bezos called his \"regret",
"freebase_image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/0mgkg?maxwidth=150&maxheight=150&key=AIzaSyBU4RGRI2UlcVoS7fEAWhKBURGp7-DCb1A",
"ebay_related_items": [
{
"title": "Amazon Kindle Fire HD 16GB, Dual-Band WiFi, 7in, Dolby Audio-Black(Latest Model)",
"image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/200_140.jpg",
"url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=3946",
"currency_id": "USD",
"current_price": "215.0"
},
{
"title": "Kindle Fire HD 7\" Dolby Audio Dual-Band Wi-Fi 16GB w/ Special Offers from Amazon",
"image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/201_140.jpg",
"url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=3966",
"currency_id": "USD",
"current_price": "170.0"
}
]
}
]
This works when I'm using json_decode($json, true)
when I'm outside of wordpress. It properly converts to an array without any problems.
But when inside of wordpress I have to do the following in order to get a proper json string:
$valid_json = str_replace('\\\n', '\n', $json);
$data = json_decode($valid_json, true);
The value stored in $valid_json
perfectly works outside of wordpress but inside wordpress I get NULL
returned to me.
I tried using var_dump()
to check to see if I'm getting the exact same thing:
$post_id = get_the_ID();
$results_cache = get_results($post_id);
$template_name = $results_cache->template_name;
$json = trim($results_cache->result);
$json = str_replace('\\\n', '\n', $json);
var_dump($json);
var_dump($json_str);
For the first var_dump I'm getting string(9841)
. And for the second which is the string that I copied directly from the database I get string(9839)
Another thing that I've noticed is that when I copy directly what is stored in the database
and use json_decode()
inside of wordpress it works without using:
str_replace('\\\n', '\n', $json);
the function that I used to fetch data from the database uses $wpdb
function get_results($post_id){
global $wpdb;
$post = $wpdb->get_row("
SELECT result, template_name
FROM results
WHERE post_id = '$post_id'
");
return $post;
}
I don't know what's the difference between fetching from the database using a query and directly copying what's on the database.
Is there anything else that I need to do for this to work?