Solved

What do I do to make this output to JSON?

Posted on 2013-01-09
18
401 Views
Last Modified: 2013-01-10
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
0
Comment
Question by:Donnie Walker
  • 8
  • 4
  • 3
  • +1
18 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 38759015
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.
0
 

Author Comment

by:Donnie Walker
ID: 38759074
yes, I've tried that and some others and it stills says there is a formatting problem.
0
 
LVL 15

Expert Comment

by:gplana
ID: 38759099
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.
0
 

Author Comment

by:Donnie Walker
ID: 38759143
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.
0
 
LVL 15

Expert Comment

by:gplana
ID: 38759329
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

0
 

Author Comment

by:Donnie Walker
ID: 38759380
I don't understand.

What about the aaData thing in the original Coldfusion?
0
 
LVL 15

Expert Comment

by:gplana
ID: 38759507
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.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38760031
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?
0
 

Author Comment

by:Donnie Walker
ID: 38760491
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
0
 

Author Comment

by:Donnie Walker
ID: 38760556
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}"
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38760645
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);
0
 

Author Comment

by:Donnie Walker
ID: 38760977
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38761526
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
0
 

Author Comment

by:Donnie Walker
ID: 38761534
Ray, all the code is needed.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38761563
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!
0
 

Author Comment

by:Donnie Walker
ID: 38761575
If you are not here to help with this problem then please move along.
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38761786
Hi rentawebguy,

1. The issue is that you are encoding and printing the JSON inside your loop. The approach is to use the loop to populate the array first. After the loop is when you echo the json_encode($array).

So basically change:

$array["aaData"][]  = array(
    'sn' => $sn,
    'givenname' => $givenname,
    'samaccountname' => $samaccountname,
    'telephonenumber' => $telephonenumber,
    'physicaldeliveryofficename' => $physicaldeliveryofficename,
    'department' => $department,
    'title' => $title
);

echo json_encode($array);
}

Open in new window


To:
$array["aaData"][]  = array(
    'sn' => $sn,
    'givenname' => $givenname,
    'samaccountname' => $samaccountname,
    'telephonenumber' => $telephonenumber,
    'physicaldeliveryofficename' => $physicaldeliveryofficename,
    'department' => $department,
    'title' => $title
);

}

echo json_encode($array);

Open in new window


2. Ray's just trying to teach you to fish. There's a reason he's the top PHP expert - it's usually a good idea to try out his advice.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38762534
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
0

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now