JVG
JVG

Reputation: 21150

Turning a string into a javascript object

I'm calling in data from the Google Sheets API, and each row's content looks like this:

 {
   $t: "title: Test Title, gamelabel: Test Game, startdate: 2016-06-14"
 }

Is there any off-the-shelf / easy way to turn this string into a javascript object in this format?

{
 title : 'Test Title',
 gamelabel: 'Test Game',
 startdate: '2016-06-14
}

Note, the keys need to be dynamic (creating keys for whatever the sheets' heading is), so knowing exactly what keys will be in $t isn't possible.

Update: Using JSON.parse() doesn't work here, I suppose there's a hacky-ish way of doing this via:

 var temp = {};
 var params = $t.split(/:/g);
 for(var i = 0; i<params.length; i += 2){
  temp[params[i].trim()] = params[i + 1].trim(); 
 }

This doesn't actually work for the supplied format / is potentially a start but I dunno what's the best practice here.

Upvotes: 0

Views: 70

Answers (5)

RobG
RobG

Reputation: 147343

If you are confident of the structure, then split on colon and comma and use reduce to create the object:

var obj = {$t:'title: Test Title, gamelabel: Test Game, startdate: 2016-06-14'};

console.log(obj.$t.split(/[:,]/).reduce(function(acc, v, i, arr) {
    if (i%2) acc[arr[i-1].trim()] = v.trim();
    return acc;
  },{}));

Or if you're into obfuscated code (not recommended) and ECMAScript 2015 environment (not widely available yet):

var obj = {$t:'title: Test Title, gamelabel: Test Game, startdate: 2016-06-14'};
console.log(obj.$t.split(/[:,]/).reduce((acc,v,i,arr)=>i%2?(acc[arr[i-1].trim()]=v.trim())&&acc:acc,{}));

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98871

Assuming the colon and commas have a pattern, you can use something like:

t = "title: Test Title, gamelabel: Test Game, startdate: 2016-06-14"
var rv = {};
for (var i = 0; i < t.split(/, /).length; ++i){
  rv[t.split(/, /)[i].split(/: /)[0]] = t.split(/, /)[i].split(/: /)[1];
}
console.log(rv)

Upvotes: 0

JonSG
JonSG

Reputation: 13067

Take the json from your spreadsheet and break it down and build it back up into an array of objects

var originalData = {
  "somerandomId" : "title: Test Title, gamelabel: Test Game, startdate: 2016-06-14",
  "someotherId" : "title: Test Title2, gamelabel: Test Game2, startdate: 2216-06-14"
};

var finalData = [];

for (var key in originalData) {
  var tmpData = {};

  originalData[key].split(",").forEach(function(item, index){
    var items = item.split(":");
    tmpData[items[0].trim()] = items[1].trim();
  });

  finalData.push(tmpData);
}

console.log(finalData);

Upvotes: 1

Bryan Chen
Bryan Chen

Reputation: 46578

You can parse it easily with split assuming , and : will never appear in the key or value part.

$t.split(',')
    .map(s => s.split(':'))
    .reduce((o, s) => {
        o[s[0].trim()] = s[1].trim();
        return o;
    }, {});

Upvotes: 5

boehm_s
boehm_s

Reputation: 5534

The keys are dynamic but the structure has to remain the same :

{
xxx: "abc",
yyy: "bcd", ...
}

var str = "title: Test Title, gamelabel: Test Game, startdate: 2016-06-14";
var comaSplit = str.split(','), i, arr = [], obj = {};
for (i=0; i < comaSplit.length; i++)
  arr.push(comaSplit[i].split(':'));
for (i=0; i < arr.length; i++)
  obj[arr[i][0]] = arr[i][1];

Upvotes: 1

Related Questions