0

I have a string passed from server which was called with AJAX and I have to convert the string into a nested array which will be used to populate on PDF.

For example:

var tableData = "[{ name: 'Bartek', age: 34 },{ name: 'John', age: 27 },{ name:'Elizabeth', age: 30 }]";

and I need to convert into an array in JavaScript which will be like this:

var newTableData = [
    { name: 'Bartek', age: 34 },
    { name: 'John', age: 27 },
    { name: 'Elizabeth', age: 30 }
];

How can I do that?

freginold
  • 3,946
  • 3
  • 13
  • 28
Ardi Sugiarto
  • 85
  • 1
  • 8
  • 5
    @Carcigenicate **But that's not valid JSON**. – ibrahim mahrir Jul 18 '17 at 13:48
  • 1
    Please post your code you have tried so far, preferably in a working snippet demonstrating the problems you are currently having so we can see what might be the cause for them. Also, please also see [**How do I ask a good question**](https://stackoverflow.com/help/how-to-ask) and, if applicable, [**How to create a Minimal, Complete, and Verifiable example**](https://stackoverflow.com/help/mcve) – Nope Jul 18 '17 at 13:48
  • Actually, after looking at it, @ibrahimmahrir is correct, the properties are not encased in quotes. – George Jul 18 '17 at 13:49
  • @Carcigenicate There is no quotes surrounding keys and the quotes used to surround value are single `'` not double `"`. – ibrahim mahrir Jul 18 '17 at 13:50
  • @Carcigenicate The outer `{}` are not needed as this is an array not an object. – ibrahim mahrir Jul 18 '17 at 13:51
  • sorry it's my first time using JavaScript so i got kinda confused... – Ardi Sugiarto Jul 18 '17 at 13:51
  • @ArdiSugiarto Is the string exactly like that? How does the server generate it? – ibrahim mahrir Jul 18 '17 at 13:52
  • @ibrahimmahrir actually i added the bracket manually from server so i can change it if i wanted to, but the point is to convert all those data into readable array on JavaScript. – Ardi Sugiarto Jul 18 '17 at 13:54
  • @ArdiSugiarto If it was a [**valid JSON**](https://en.wikipedia.org/wiki/JSON), it will be easy: just use `var newTableData = JSON.parse(tableData);`. – ibrahim mahrir Jul 18 '17 at 13:57
  • 1
    How the string get generated on the server side? Can you change that? – ibrahim mahrir Jul 18 '17 at 13:59
  • @ibrahimmahrir the string was generated from database query and saved on string, then send back to client side using servlet. – Ardi Sugiarto Jul 18 '17 at 14:21

3 Answers3

2

As pointed out in the comments, the best solution would be to return a valid JSON from the server and to parse it using JSON.parse.
You can use tools like https://jsonlint.com/ or JSV to check that your JSON is valid.

If because of some "real world problem", your servers aren't JSON complaint, you can use a dirty parser like dirty-json or write your own JSON parse.

dirty-json does not require object keys to be quoted, and can handle single-quoted value strings.

var dJSON = require('dirty-json');
dJSON.parse("{ test: 'this is a test'}").then(function (r) {
    console.log(JSON.stringify(r));
});

// output: {"test":"this is a test"}

Your last resort, while technically possible and the easiest to implement, is probably your worst choice because of it's dangers. but it would work out of the box: eval.

eval(tableData);
// [ { name: 'Bartek', age: 34 },
//   { name: 'John', age: 27 },
//   { name: 'Elizabeth', age: 30 } ]
LifeQuery
  • 3,202
  • 1
  • 26
  • 35
0

By slightly changing how you return the string from the server you can JSON.parse it

var dataString = '[{"name":"Bartek","age":34},{"name":"John","age":27},{"name":"Elizabeth","age":30}]';
var data = JSON.parse(dataString);
console.log(data);
0

Use eval() method The completion value of evaluating the given code. If the completion value is empty, undefined is returned:

var tableData = "[{ name: 'Bartek', age: 34 },{ name: 'John', age: 27 },{ name:'Elizabeth', age: 30 }]";

tableData = eval(tableData);
console.log(tableData[0]);
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90