SOLVED - Repeatable fail of json_encode output in CF4

Joe Stokes 20 Nov, 2013
Hi guys,
I've been struggling with this one for days now and I believe I've found where the problem is caused.

This is mainly when using json_encode and then echo the result string to the browser to be used as JSON into jQuery and ajax.

Here is a simple example that demonstrates the problem quite well and repeatedly.

I created a new form and used the section of code (between php tags) as the code in the form html. In the wizard I dragged a show html action into onLoad, saved the form and tested it.

The JSON string shows only [,,] (empty) and all other outputs are as expected - an array of stdClass objects.

After a few days of research on the web and many trials, I finally tried the code on its own by accessing the json_test.php directly from the browser. It works just fine!

So IMO the problem either arises in CF when it does it's magic behind the scenes or in Joomla. Somehow the JSON string is stripped out along the way.

What is amazing is that not every JSON string is affected. This is where it becomes very difficult to find what is happening. I've also posted a question about this herehttp://stackoverflow.com/questions/19987445/php-echo-command-produces-incomplete-object-definition-resulting-in-json-parsing

This sample piece of code below reliably produces this problem when used in CF, but produces the correct output when accessed on it's own.

Does any one have any idea or suggestions to overcome this problem?

Here is the sample code for json_test.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head></head>
	<body>
	
		<?php
		// This is the array
		$gridColProp = array 
			(
			array('title'=>"Item",	'width'=>"30px",	'justify'=>"right",		'strLen'=>4,	'action'=>"none"),
			array('title'=>"Date",	'width'=>"70px",	'justify'=>"center",	'strLen'=>10,	'action'=>"none"),
			array('title'=>"Name",	'width'=>"140px",	'justify'=>"left",		'strLen'=>18,	'action'=>"none")
			);	
			
		// Print the array to screen
		echo '$gridColProp as output from print r</br />';
		echo '<pre>'.print_r($gridColProp, true).'</pre>';

		// Do the JSON encoding and then print to screen first using print_r then with echo
		$jsonGridColProp = json_encode($gridColProp);
		echo '$jsonGridColProp as output from print r after json_encode</br />';
		echo '<pre>'.print_r($jsonGridColProp, true).'</pre>';

		echo '$jsonGridColProp as output from echo after json_encode</br />';
		echo $jsonGridColProp;
		echo '<br />';

		// Use the JSON encoded string variable and rebuild the array
		$objectFromJson = json_decode($jsonGridColProp);						// Rebuild object from "empty" JSON
		echo '$objectFromJson as output from print r after json_decode</br />';
		echo '<pre>'.print_r($objectFromJson, true).'</pre>';
		?>
		
	</body>
</html>
GreyHead 20 Nov, 2013
Hi Joe Stokes,

I've used JSON a fair bit with ChronoForms without any major problems. Usually I've had the code in a Custom Code box either to re-send back to an Ajax request or to save a data dump in a database table.

What exactly is the PHP you are using and where is it in your form?

Bob
Joe Stokes 20 Nov, 2013
Hi Bob,

Yes, JSON in CF does work very well. I'm using it very extensively to pass quite a large dataset (big object) to jQuery. In essence, this web application is MVC based with the only difference that the controller is implemented in jQuery and not at the server side.

The model and base-line views are server side and the controller is implemented in jQuery to enable easy user navigation and control (add and edit complex data sets with regular dynamic data interaction happening).

Passing data between the server and browser through ajax and json has been an absolute pleasure so far. However, I started getting JSON parsing errors in jQuery and in the process stumbled upon this problem.

In one of my experiments, I found that by just adding another field (not really needed) in the SQL that produces the object which I pass through to the browser via JSON, the resulting JSON is correct but by not including this field in the data, the JSON of the sub-object is malformed (empty). I have yet to get to identify the pattern that causes this.

It has been through repeated trials that I stumbled upon this example that repeatedly reproduces the problem. In this example, the JSON string produced, does not "echo" or "print_r". However, when decoding this JSON string back (which one would think is empty), the array of stdClass objects is created. I know it is very strange! (A few days of "What???? That's impossible!!!!" testifies to that).

What I can suggest is that you (if you have the time) just experiment with the simple sample code I posted and try to see if you have the same results. On my side I've tried it on two separate hosting services, one using php 5.3 and the other 5.4 and had the exact same results. Use the php section in a test CF and test. Then use the full example code and call it directly from the browser.

It would be interesting to hear what you experience.

ps. I can send you the log-in details for this application via pm - it is still in early days but I think you may find it interesting as to how far one can push CF!.

Thanks for the reply
Joe
Joe Stokes 20 Nov, 2013
Hi Bob

I've just tried this same piece of sample code in K2 Tools as custom code and it worked fine.

It does appear as if the JSON output is stripped in CF under "some special circumstances". The challenge for me is to try and identify when and how it happens.

This was tested on another J2.5 test site with K2 and CF. CF produces the problem, K2 custom code utility works just fine. So I do not believe that the JSON is stripped anywhere else.
Joe Stokes 20 Nov, 2013
Hi Bob,

GREAT NEWS - PROBLEM SOLVED

Having isolated the problem area, I just had a thought to substitute the standard "show HTML" action with the "show HTML (GH)" action, and voila! No more problems.

I will try this now on my application implementation to see how it works and will give feedback.
Joe Stokes 20 Nov, 2013
Hi Bob,

I've just tried the "show HTML [GH]" action on my application instead of the standard one and the problem is no more!

So perhaps this will help others that run into a similar problem with json_encode.

Regards
Joe
This topic is locked and no more replies can be posted.