-3

This question is similar to string-to-json-array-of-json-objects. I have the following string:

"[{'Phonetype':'Pre','Phone':'918282311'},{'Phonetype':'pre','Phone':'918333222'}]"

How to convert this string to an array of objects using node?

Am reading data from a CSV file using csvtojson. Excel sheet contains a field phoneNumber. This field contains the above mentioned data. Am getting this data as string from the csv file. I want the data as JSON.

I tried JSON.parse(string). But I got the following error:

Unhandled rejection SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)

Code:

csv().fromFile(csvFilePath)
.then((jsonObj)=>{
    var a = jsonObj[0].phoneNumber;
    console.log(a);
    console.log(JSON.parse(a));
});

Here console.log(a) prints [{'Phonetype':'Pre','Phone':'918282311'},{'Phonetype':'pre','Phone':'918333222'}] as a string. When I try to convert into array of objects in the next line, it throws exception.

Anil Kumar
  • 18
  • 5
  • 6
    If that is a string then it **is** a JSON array already. – Quentin Sep 16 '18 at 17:50
  • @Li357 — It's entirely possible that the OP intended to ask for that though. Their question doesn't really make sense in its current form. – Quentin Sep 16 '18 at 17:52
  • If you want the data as a JavaScript array (rather than a JSON array), why not use a CSV library designed to *just parse CSV*? Why convert it to JSON and then convert it (again) to JavaScript? – Quentin Sep 16 '18 at 17:53
  • 1
    "But I got the following error" — I don't get that error: http://jsbin.com/joharosehe/1/edit?js,console — perhaps `string` doesn't contain the value you think it does. – Quentin Sep 16 '18 at 17:55
  • 1
    Please read [What is the difference between JSON and Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) and update your question accordingly as it is really unclear. JSON *is* a string by definition. – str Sep 16 '18 at 17:56
  • For your third comment @Quentin , I believe that the CSV he is parsing has a column that contains JSON. His error could be from trying to parse the column heading (if the CSV has a heading row) or any single row that has invalid JSON in a loop. – Paul Sep 16 '18 at 18:00
  • Question updated with code I tried. @Li357 – Anil Kumar Sep 16 '18 at 18:08
  • The string you currently have is invalid JSON (it is a valid syntax for javascript object literal syntax however it is invalid JSON data format). The character `'` is an invalid token in JSON. You need `"` – slebetman Sep 16 '18 at 18:21
  • In the CSV document it is `'`. And I cannot edit that. So is there anything I can do in the coding side to get it as JSON? @slebetman – Anil Kumar Sep 16 '18 at 18:24
  • Then it is not JSON. It is javascript syntax. You have two choices: 1 write your own parser and parse the input byte-by-byte. 2 use eval – slebetman Sep 16 '18 at 18:26

2 Answers2

1

If it contains 'single quotes' instead of "double quotes" then it is not valid JSON.

You will need to do a .replace(/'/g, '"')

console.log(JSON.parse("[{'Phonetype':'Pre','Phone':'918282311'},{'Phonetype':'pre','Phone':'918333222'}]".replace(/'/g, '"')));
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
0

JSON.parse works fine on that string.

console.log(JSON.parse('[{"Phonetype":"Pre","Phone":"918282311"},{"Phonetype":"pre","Phone":"918333222"}]'));

Your data is already an object and doesn't need parsing, try

csv().fromFile(csvFilePath)
.then((jsonObj)=>{
    var a = jsonObj[0].phoneNumber;
    console.log(a);
    console.log(a[0].Phonetype);
    console.log(a[0].Phone);
    console.log(a[1].Phonetype);
    console.log(a[1].Phone);
});
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60