0

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.

9
  • 1
    @RobG: No; that isn't JSON. Commented May 31, 2016 at 2:41
  • That looks like a job for eval() Commented May 31, 2016 at 2:41
  • I edited in a semi-code sample into the question Commented May 31, 2016 at 2:49
  • check here stackoverflow.com/questions/45015/… Commented May 31, 2016 at 2:52
  • How are commata escaped in the field values? Commented May 31, 2016 at 2:54

5 Answers 5

5

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;
    }, {});
Sign up to request clarification or add additional context in comments.

Comments

1

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];

Comments

1

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);

Comments

1

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,{}));

Comments

0

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)

Comments

Your Answer

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.