Click here to Skip to main content
15,041,548 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a json with PHP code as shown below.

How remove square brackets from service details (ie,inner square brackets used for storing service details)

PHP code:
PHP
while($row1=$check1->fetch_assoc())
{
$empleadresult['empname']=$row['empname'];
$empleadresult['empemailid']=$row1['empemailid'];
$CheckSql2 = "SELECT * FROM leadservices,services WHERE leadservices.leadslno='$leadsl'";
$check2=$con->query($CheckSql2);
while($row2 =$check2->fetch_assoc())
{
 $output2[] = array("service_id" => 
 $row2['service_id'],"service_name" => 
 $row2['service_name']);
}
$empleadresult['services']=$output2;
$leadarray[] = $empleadresult;
$trimmed['LeadDetailsObject'] = str_replace('\r\n','', $eadarray);
echo json_encode($trimmed,JSON_UNESCAPED_SLASHES);


What I have tried:

JavaScript
{
  "LeadDetailsObject":[
	{
		"empname": "Anuj Sharma",
		"empemailid": "anuj@toplinebiz.com",
		"service": [
		   {
			"service_id": "6",
			"service_name": "Landscaping And Gardening"
		   },
		   {
			"service_id": "2",
			"service_name": "Civil Finishing And Tumkey Civil Projects"
		   }
		]
      }
  ]
}
Posted
Updated 24-Mar-21 3:27am
v2

1 solution

Looking at the JSON, you can't remove the inner square brackets: they define the collection under "service", and since that contains two elements you can't discard the enclosing square brackets without making the resulting JSON invalid and unreadable.

Why would you think that was a good idea?
   
Comments
Mary Reejo R 24-Jul-20 6:56am
   
I know this is not a good idea.But in ios the inner square brackets not accepting. Is there any other idea ?
OriginalGriff 24-Jul-20 7:13am
   
Use a "proper" json processor that produces classes instead of "rolling your own"?
https://www.php.net/manual/en/function.json-decode.php
Mary Reejo R 25-Jul-20 0:49am
   
THANK YOU FOR YOUR REPLY..
Is this correct way of displaying output?

{
"SurveyObject": {
"EmpOject": [
{
"id": "1",
"leadslno": "4",

}
],
"SubObject": [
{
"id": "2",
"leadslno": "4",

}
]
}
}
OriginalGriff 25-Jul-20 2:01am
   
Don't ask me what your output should be - I have no idea what you are trying to achieve!


Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with, we get no context beyond that at all.
Mary Reejo R 25-Jul-20 7:51am
   
Sorry sir...i am just asking about json format.Is it correct format or not ?
OriginalGriff 25-Jul-20 8:00am
   
It's valid JSON, yes. Is it what you wanted? No idea! :laugh:

If you want to check JSON, there are online code converters:
https://json2csharp.com/
Is the one I use most often - it checks the JOSN and generates the relevant C# classes if it's OK. There are probably PHP equivalents out there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900