I have a JS literal object string such as {name:{first:"George",middle:"William"},surname:"Washington"}
and I have to convert it in Json. How can I do it using PHP?
Asked
Active
Viewed 5,510 times
16

tic
- 4,009
- 15
- 45
- 86
-
possible duplicate of [How to parse a JSON string using PHP](http://stackoverflow.com/questions/5527065/how-to-parse-a-json-string-using-php) – Sergiu Paraschiv Sep 19 '13 at 15:09
-
2That's kind of strange having a string of a js object literal in php. – Musa Sep 19 '13 at 15:11
-
possible duplicate of [how to decode this JSON string?](http://stackoverflow.com/questions/2235374/how-to-decode-this-json-string) – epascarello Sep 19 '13 at 15:11
-
3This is not a duplicate question. JSON and object literals are not the same thing and json_decode doesn't parse object literal strings. – Aristona Dec 17 '14 at 22:53
-
because the key names are not quoted in the JS literal string, this could get ugly. I suspect you'll have to write your own parser similar to the JS runtime interpreter's parser, which is a complex and buggy proposition. Perhaps you can simplify the grammar to accept only a subset of JS object literals, which would make it possible to do the job with a reasonable set of regexps? – RobP Feb 25 '15 at 17:33
-
This is uncommon but still something that occurs when scrapping websites – Borjante Oct 27 '17 at 14:45
-
What **exactly** do you want to achieve? Convert this to....? – Nico Haase Feb 19 '21 at 12:41
4 Answers
5
If someone is still looking for an easy solution to this, as I did recently, you could check out the PHP library that I wrote: ovidigital/js-object-to-json
1) Install with composer
composer require ovidigital/js-object-to-json
2) Use it inside your project
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($javascriptObjectString);

ovi
- 431
- 5
- 11
3
JS:
// Pretend we're POSTing this
var foo = {foo:{first:"George",middle:"William"}};
PHP:
$foo = $_POST['foo'];
$foo = json_decode( stripslashes( $foo ) );
echo $foo->first;
Credit where credit is due: https://www.youtube.com/watch?v=pORFYsgOXog

adamj
- 4,672
- 5
- 49
- 60
0
If you are lucky enough to know what the keys will be when they arrive on your script, and you know that they won't appear within the values, you could do this with str_replace()
to add double quotes to the keys:
$notQuiteJson = '{name:{first:"George",middle:"William"},surname:"Washington"}';
$initialJson = array('name:','first:','middle:','surname:');
$replacedJson = array('"name":','"first":','"middle":','"surname":');
$convertedDataString = str_replace($initialJson, $replacedJson, $notQuiteJson);
$actualJson = json_decode($convertedDataString);
Far from perfect, but hopefully this helps someone.

Orchis
- 296
- 5
- 18
-
I'll take this as an answer. Works for where I need it: get data out of a javascript file. – Joeri Jun 10 '22 at 11:19
-4
Not json_encode, use $var = json_decode($_POST['names'], true)
. You can then use it like echo $var['surname']
to echo "Washington".

Connor Peet
- 6,065
- 3
- 22
- 32
-
4This is not a JSON formatted string so your answer doesn't work. This is an object literal in string format, so your solution would actually output null. – Aristona Dec 17 '14 at 22:56