Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've some PHP source code that are simple key-value arrays like these:

return array('var1' => 'var2' );

And

return array('sub' => array( 'var1' => 'var2' ) );

And I need to parse them into JavaScript objects, because I've a JavaScript implementation of a PHP library and I want to test the compatibility using the original test cases.

There are over a 100 tests, so manual conversion is not practical.

Is there an easy way to convert these into JavaScript objects without using PHP?

share|improve this question
1  
Why not use JSON? Why without PHP? –  elclanrs Apr 20 '13 at 8:28
    
Json is fine but since the test framework runs in a node.js app PHP isn't a good option. –  mabs Apr 20 '13 at 11:24

4 Answers 4

up vote 1 down vote accepted

To actually answer your question – how to parse PHP's associative arrays to JSON without using PHP – let's use some JavaScript code.

"array('sub' => array( 'var1' => 'var2' ) );".replace(/array\(/g, '{').replace(/\)/g, '}').replace(/=>/g, ':').replace(/;/g, '').replace(/'/g, '"');

This is assuming you just happen to sit on some source code and want to copy it into a Node.js application, and that all the data looks exactly like this. If the data happens to be on multiple lines, if you even want to parse away the "return"/";" parts, if some of the data contains indexed arrays, or if any of the values contain the string I just naively parse away, you'll have to make this script a bit smarter.

And as others have said, if you're interacting with a PHP service, just use json_encode().

share|improve this answer
1  
Thanks, that's a lot simpler than I thought it would be. –  mabs Apr 22 '13 at 3:03

This should work!

<?# somefile.php ?>
<script type="text/javascript">
    var json = '<?= json_encode(array('sub' => array( 'var1' => 'var2' ))) ?>';
    var object = JSON.parse(json);
    console.log(object);
</script>

Output

{
  sub: {
    var1: "var2"
  }
}

More commonly, you will see a language-agnostic API which simply provides JSON responses. You could easily test this using asynchronous requests to the API from JavaScript (in-browser, via Node.js, etc.);

share|improve this answer
1  
is there a need for JSON.parse ? what if you write var object = <?php echo json_encode(array('sub' => array( 'var1' => 'var2' ))) ?>; instead? –  user2264587 Apr 20 '13 at 9:12
    
Object literals are not defined in JSON; they are defined in JavaScript's object literal syntax. They are similar, but they are not the same. –  naomik Apr 20 '13 at 9:14
    
I thought JSON was a subset of object literals syntax, therefore if something is valid JSON it would be a valid literal? –  user2264587 Apr 20 '13 at 9:17
1  
Yeah, almost, but it's not. Just parse the JSON as intended and you won't have any strange side effects. –  naomik Apr 20 '13 at 9:20
    
you're right! also, in your link it shows a valid JSON string that isn't even a valid javascript string (after quoting it). not that I understand why it's like that.. –  user2264587 Apr 20 '13 at 9:40

If I understood you correctly, you need to use json_encode() like this:

return json_encode(array('sub' => array( 'var1' => 'var2' )));

The returned value: {"sub":{"var1":"var2"}}

share|improve this answer

You can use json_encode() in PHP and JSON.parse() in JavaScript.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.