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?

11
  • 5
    @Carcigenicate But that's not valid JSON. Commented Jul 18, 2017 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 and, if applicable, How to create a Minimal, Complete, and Verifiable example Commented Jul 18, 2017 at 13:48
  • Actually, after looking at it, @ibrahimmahrir is correct, the properties are not encased in quotes. Commented Jul 18, 2017 at 13:49
  • @Carcigenicate There is no quotes surrounding keys and the quotes used to surround value are single ' not double ". Commented Jul 18, 2017 at 13:50
  • 1
    How the string get generated on the server side? Can you change that? Commented Jul 18, 2017 at 13:59

3 Answers 3

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

Comments

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

Comments

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

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.