• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

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
0
Donnie Walker
Asked:
Donnie Walker
  • 8
  • 4
  • 3
  • +1
1 Solution
 
gplanaCommented:
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
 
Donnie WalkerAuthor Commented:
yes, I've tried that and some others and it stills says there is a formatting problem.
0
 
gplanaCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Donnie WalkerAuthor Commented:
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
 
gplanaCommented:
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
 
Donnie WalkerAuthor Commented:
I don't understand.

What about the aaData thing in the original Coldfusion?
0
 
gplanaCommented:
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
 
gr8gonzoConsultantCommented:
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
 
Donnie WalkerAuthor Commented:
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
 
Donnie WalkerAuthor Commented:
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
 
gr8gonzoConsultantCommented:
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
 
Donnie WalkerAuthor Commented:
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
 
Ray PaseurCommented:
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
 
Donnie WalkerAuthor Commented:
Ray, all the code is needed.
0
 
Ray PaseurCommented:
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
 
Donnie WalkerAuthor Commented:
If you are not here to help with this problem then please move along.
0
 
gr8gonzoConsultantCommented:
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
 
Ray PaseurCommented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now