18

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 Sep 19, 2013 at 15:08
7
  • possible duplicate of How to parse a JSON string using PHP Commented Sep 19, 2013 at 15:09
  • 2
    That's kind of strange having a string of a js object literal in php. Commented Sep 19, 2013 at 15:11
  • possible duplicate of how to decode this JSON string? Commented Sep 19, 2013 at 15:11
  • 4
    This is not a duplicate question. JSON and object literals are not the same thing and json_decode doesn't parse object literal strings. Commented Dec 17, 2014 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? Commented Feb 25, 2015 at 17:33

4 Answers 4

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);
answered Apr 23, 2020 at 11:43

Comments

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

answered Dec 2, 2015 at 2:09

Comments

1

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.

answered Oct 16, 2019 at 9:05

1 Comment

I'll take this as an answer. Works for where I need it: get data out of a javascript file.
-4

Not json_encode, use $var = json_decode($_POST['names'], true). You can then use it like echo $var['surname'] to echo "Washington".

answered Sep 19, 2013 at 15:12

1 Comment

This 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.