0

*This question was created as I had no control over the JSON output at the time. So had to use JavaScript. If you have control over the JSON, refer to Mike Brant's answer. But Oka's answer solved my issue below and is a great solution..

I'm trying to create an array that doesn't contain double quotes from JSON.

I'm getting JSON and trying to build a system where I can push non quoted items to the array.

As I have no control over the JSON, I'm making it into a string and removing the double quotes and splitting it into an array again.

The problem is this still outputs the double quotes?

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';
var artistIds = artistJSON.replace(/"/g, '');
var artistAry = artistIds.split(',');
console.log(artistJSON);
console.log(artistIds);
console.log(artistAry);

Results from console;

["31","41","56","38","","27"]
[31,41,56,38,,27] //This is a string. I want an array.
["[31", "41", "56", "38", "", "27]"]

https://jsfiddle.net/1pu6nqu2/

Any help would be very grateful.

*Just to confirm, my aim of the game is to remove the double quotes from within the array.

TidyWorks
  • 43
  • 1
  • 6

5 Answers5

2

If you are using json_encode() from PHP to dynamically populate the data structure, you should not populate into a string and then parse that string, just write directly to object/array literal. So change this:

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';

to this

var artist = <?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>;

All I did was remove the single quotes (and change the variable name to something more appropriate). Now you have a data structure you can work with directly in javascript without need for additional parsing.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • I'm aware of removing the single quotes to get an array, the problem is how do I remove the double quotes from the array? – TidyWorks May 06 '15 at 14:22
  • @TidyWorks Well, you should structure the data in the array/object with the proper data types before encoding. So if you have numeric values represented as strings, you should cast them to appropriate numerical type in PHP BEFORE encoding to JSON. That way, the data types will transfer seemlessly to javascript via the JSON serialization. Remember, JSON is just a serialization method. It is on you as programmer to make sure you get your data into the proper structure/types, before performing the serialization if you don't want to have to deal with unexpected types in the serialization result. – Mike Brant May 06 '15 at 14:24
  • Pretty sure the double quote's are happening because you're re-encoding a JSON literal data structure already and spitting it out in PHP's json_encode as a string. Where is your origin of your data coming from the database, or what? Specifically `$favourites->artists`, what is the value of this in a normal string? – NiCk Newman May 06 '15 at 14:25
  • Sounds like your array values are strings and not integers. That's why they're getting quotes. Try parsing them into integers – casraf May 06 '15 at 14:25
2

Assuming you're trying to turn stringified JSON into an array, and turn the strings inside the array into numbers. You can use some combination of .map() and .filter() to achieve this.

http://jsbin.com/yojibeguna/1/edit?js,console

var artistJSON = JSON.parse('["31","41","56","38","","27"]')
                     .map(function (e) { return parseInt(e); })
                     .filter(function (e) { return isFinite(e); });

console.log(artistJSON, typeof artistJSON[0]);
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 1
    This works brilliantly. Now I can sleep. If I could buy you a beer, I would. – TidyWorks May 06 '15 at 14:39
  • 1
    I know this is accepted answer, but I would point out to those who may read this in the future, that you would generally be better off to get your data structure correct before serializing to JSON. That way you are not splitting logic on assembling/typing the data structure between your backend and your frontend. – Mike Brant May 07 '15 at 13:58
  • @MikeBrant Completely agree with you, but it should be noted the original question stated no control over the `JSON` data they were being delivered. Sometimes compromises need to be made, and sometimes your front-end does need to fix what your back-end can't, or won't. – Oka May 07 '15 at 15:28
1

If the json data is stored as JSON data already, you do not need to re-encode it with php. Just echo it out and it will be assigned to your variable artistJSON.

Example:

var artistJSON = <?php echo $favourites ? ($favourites->artists) : '[]' ?>;

Edit: As Mike Brant said, you do need to re-encode it if it's not already stored as JSON literal data (in a db, or whatnot). I'm assuming it is.

NiCk Newman
  • 1,716
  • 7
  • 23
  • 48
1

Try this:

var artistJSON = '<?php echo $favourites ? json_encode($favourites->artists) : '[]' ?>';
var artists = JSON.parse( artisanJSON );

console.log( artists );

REF: How to json_encode php array but the keys without quotes

Community
  • 1
  • 1
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

You can just run JSON.parse to convert the string to an array.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

David Nguyen
  • 8,368
  • 2
  • 33
  • 49