javascript, remove double quotes inside multiple array

181 Views Asked by At

I am trying to build a custom visual for datastudio, I got it working but there is a step, i can't fix, Datastudio generate a json file like this

export const message = {
  "tables": {
    "DEFAULT": [
      {
        "coordinateid": [
          "143.4999336,-34.777302"
        ],
        "colorid": [
          "169,255,169"
        ],
        "sizeid": [
          1
        ]
      },
      {
        "coordinateid": [
          "143.4999358,-34.7773749"
        ],
        "colorid": [
          "169,169,169"
        ],
        "sizeid": [
          1
        ]
      },
      {

in deckgl the relevant code is

const drawViz = (data) => {
       var data1 = data.tables.DEFAULT;  

    getPosition: d => d.coordinateid,

the only way to make it work, is when i remove the quotes from the values inside the array

"coordinateid": [
          143.4999336,-34.777302
        ]

is there a way either to remove the double quotes between the bracket or a way just to parse the values and ignoring the double quotes

2

There are 2 best solutions below

0
On BEST ANSWER

DataStudio returns GEO LatLong coordinates as a comma separated string. The correct way to parse this would be the following:

var baseCoordinate = "143.4999336,-34.777302";
// Split out the coordinates into multiple strings
var coordinates = baseCoordinate.split(",");
// Turn the strings into floats
var coordinatesAsNumbers = coordinates.map((coord) => parseFloat(coord));

This will give you the coordinates as floats in an array, which seems to be the format that deckgl is expecting.

0
On

find the answer

basically, change the javascript object to string using JSON.stringify the using regex to replace the strings then use Json.parese to change it back to an object

  var data1 = data.tables.DEFAULT;
  var data2 = JSON.stringify(data1);
  var data3 = data2.replace(/\"]/g, "]");
  var data4 = data3.replace(/\["/g, "[");
  var data4 = JSON.parse(data4);