Link to home
Start Free TrialLog in
Avatar of Donnie Walker
Donnie WalkerFlag for United States of America

asked on

What do I do to make this output to JSON?

I am trying to convert a Coldfusion page to PHP.

So far I have been able to connect to LDAP with PHP and get results of the user directory.

I now need to output those results so that JSON can read them. But I have no idea how to do that.

JSON is enabled in PHP.

Attached is the original Coldfusion page and the new PHP page.

Any assistance would be appreciated.
cf.txt
php.txt
Avatar of gplana
gplana
Flag of Spain image

I think all you have to do is to use the json_encode function:

http://php.net/manual/en/function.json-encode.php

This function takes a variable and converts it to a serialized string (in json format).

Also, here is the index of json php manual, but as you have already installed it I think it's not necessary to read for you, but I provideit to you  just as another reference: http://php.net/manual/en/book.json.php

Hope it helps.
Avatar of Donnie Walker

ASKER

yes, I've tried that and some others and it stills says there is a formatting problem.
Sorry, but looking at your php code, I can's see any call to function json_encode. Also, you shouldn't made any echo so content of the page is "just" a json stream.
I don't have any JSON on the php script because I do not know how to take the code on the coldfusion script and make it work the same on the php script.

I need a working example.
This is a very simple example:

Hope it helps.

$array[0] = 'First element';
$array[1] = 'Second element';
$array[2] = 'Third element';
echo json_encode($array);

Open in new window

I don't understand.

What about the aaData thing in the original Coldfusion?
Sorry, I don't know Coldfusion.  I believed your question was just about generate JSON from PHP. But I think this code:

{
	"aaData": [
		<cfoutput query="q">
		[
			"#q.sn#",
			"#q.givenName#",
			"#uCase(q.sAMAccountName)#",
			"#q.telephoneNumber#",
			"#q.physicalDeliveryOfficeName#",
			"#q.department#",
			"#q.title#"
		]<cfif q.currentRow lt q.recordCount>,</cfif>
		</cfoutput>
	]
}

Open in new window

Seems to be the code that generates JSON notation. An example of JSON notation that maybe is more similar to what may generate this code is:

"aaData": [
		[
			"John",
			"Smith",
			"10000",
			"555-34-23-12",
			"MyOffice",
			"MyDepartment",
			"Senior Programmer"
		]
	]
}

Open in new window


JSon is a notation that represents [ ] for array values and { } for struct values, so in this example you have an initial struct called aaDate which has an array,  which has an array, with have some string values.

Hope it helps.
Hi,

In my original answer to your question, I included output for aaData and json_encode(). If that doesn't work, can you post the final, generated results?
Attached is the new code. It gives me an error saying:

"DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error"
php2.txt
when I echo that code I see this:

"{\r\n\"aaData\": [\r\n\t\t[\r\n\t\t\tDoe,\r\n\t\t\tJohn,\r\n\t\t\tjd38,\r\n\t\t\t1163,\r\n\t\t\tLI 168 C,\r\n\t\t\tOffice of Technology Services,\r\n\t\t\tManager\/Infrastructure\/Technology Services\r\n\t\t]\r\n\t]\r\n}"
Wait, that code you just posted (php2.txt) doesn't echo anything out.

Try what I initially suggested by adding the entries into an array, and then JSON encode the array into the string. That should work.

$ldap_results = ...;
$myarray = array();
for(each LDAP entry in $ldap_results)
{
    $myarray["aaData"][] = array("attribute1","attribute2","attribute3",etc...);
}
echo json_encode($myarray);
When I do the attached code it seems to try to output the values but it becomes an endless loop with lines repeating.
php3.txt
I think this example is unnecessarily complicated.  Start with the http://SSCCE.org example - a small and structured example illustrating the failure. Then build up from that.  Please show us the minimum amount of code and data necessary to show us your problem.  Once we have seen that we can look at the larger code and data example, but we cannot start with the larger problem -- we must start at the atomic definition of the problem.

Thanks, ~Ray
Ray, all the code is needed.
Did you read the http://SSCCE.org web site yet?  If not, please read it carefully and take its advice to heart.  This is not for my edification -- I know how to create small and structured examples.  I am trying to lead you in the direction of a debugging process that will give you a path to dependable and extensible practices.

Your initial goal is to find the part of the code where the failure occurs.  Once you have found that, create the SSCCE example.  That will get you an immediate answer from the EE community.  Then you can begin adding back the other, more complex parts of the code.  As soon as you add a part of the code that creates another failure, you have a new question to post here at EE.  Etc.  It's not rocket science, it is simple, plodding, step-by-step deterministic work.  The smaller you make the bites, the faster you can eat the elephant!
If you are not here to help with this problem then please move along.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am trying to help with the problem, but the problem is too big, and has too many moving parts (I haven't got enough free time) to take on everything at once.  I was hoping that there was some smaller part of the problem that I could help you solve, and thus make at least some progress.  

Since you do not want that kind of help, I will "move along," as you put it.  You're in good hands with gr8gonzo.

Best of luck with the project, ~Ray