PHP JSON Encode for Java-Script?

Hello,
Is it possible to encode a JSON in PHP in Java-Script Style?
As sample,

[
	{ id:6, status:"new", text:"Task 6", tags:"webix,kanban", comments:[{text:"Comment 1"}, {text:"Comment 2"}], personId: 2 },
];

Open in new window


A similar export I did is:

[
	{ "id":"6", "status":"new", "text":"Task 6", "tags":"webix,kanban", "comments":"[{"text":"Comment 1"}, {"text":"Comment 2"}]", "personId": "2" },
];

Open in new window


Special the part:
comments:[{text:"Comment 1"}, {text:"Comment 2"}]

Open in new window

looks like a JSON encoded string inside a JSON?
How is this possible to generate with an SQL query?
Ingo FoersterProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Your JSON is not valid. You can test it on this sites :
http://jsonviewer.stack.hu/
https://jsonlint.com/

The double quotes of the comment must me escaped
[
  {
    "id": "6",
    "status": "new",
    "text": "Task 6",
    "tags": "webix,kanban",
    "comments": "[{\"text\":\"Comment1\"}, {\"text\":\"Comment2\"}]",
    "personId": "2"
  }
]

Open in new window


Text, double quote escaped
viewer, double quote escaped
or removed :
[
  {
    "id": "6",
    "status": "new",
    "text": "Task 6",
    "tags": "webix,kanban",
    "comments": [{"text":"Comment1"}, {"text":"Comment2"}],
    "personId": "2"
  }
]

Open in new window


text, no double quotes
viewer, no double quotes
0
RobOwner (Aidellio)Commented:
Easiest way I've found to work with json and php is to use a php array in php and encode and decode as needed for javascript (or db storage).

http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
1
Olaf DoschkeSoftware DeveloperCommented:
Besides the two PHP functions JSON_ENCODE and JSON_DECODE Javascript itself also has JSON.stringify() and JSON.decode()

You could never send over a JS object as is anyway, any http communication of browser/client and Server, whether PHP or NODE.js, will be in form of stringified data.

So in the case the JS objects come from JS you'd rather send over JSON stringified to the server backend, which would merely store it, query it (perhaps with SQL includeing LIKE clauses) and return it, so JS can then make an object via JSON.decode().

The uses of PHPs JSON functions are good to work on the detail data in the form of PHPs native data work horses of associative arrays. JSON turned to PHP arrays can of course be processes in any PHP way you'reused to and then a resulting associative array can be stringified again.

In the end the pair of such function on both sides, JS and PHP, are what makes this a workable solution.

But don't get the idea this is just because of using two languages. If you work on client/server in the web, that always means two sides, which communicate through http protocol via string representations of objects or arrays or table rows or anything alike, in form of JSON, or XML, or other proprietary or non prorietary formats, even when it's a JS frontend with Node.js backend.

Bye, Olaf.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Julian HansenCommented:
is it possible to encode a JSON in PHP in Java-Script Style?
JSON is JSON - irrespective of where it is used.
You take your native object and run it through a JSON encode function it produces text based JSON which can then be used in any JSON aware application.

As noted above - in PHP when you want to send JSON you encode it with json_encode() example
$myobject = new stdClass;
$myobject->name = 'Test Object';
$myobject->list = array('first' => 'Item 1', 'second' => 'item 2');

$json = json_encode($myobject);
echo $json;

Open in new window

Produces the following output
{"name":"Test Object","list":{"first":"Item 1","second":"item 2"}}

Open in new window

You can see it working here
0
leakim971PluritechnicianCommented:
the two questions are :
looks like a JSON encoded string inside a JSON?
How is this possible to generate with an SQL query?

none of you comments give the author an answer talking about.
0
Julian HansenCommented:
The question is phrased badly - not really clear what the author wanted - the answers don't match because the question is not clear.

Therefore recommend delete.
1
RobOwner (Aidellio)Commented:
someone wanting to send valid json from PHP would benefit from what I've suggested. Olaf goes on to explain why. i believe those comments are useful and should be retained
0
Olaf DoschkeSoftware DeveloperCommented:
It's annoying, if not worse, that questions are requested to be closed with recommendations even before the OP has come back once. Why not be a little more patient? This mainly goes to EE moderation.

Bye, Olaf.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.