1

I've encountered a problem with conversion list of individual JavaScript object to proper JavaScript array.

The list looks like below:

{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }

So, as you can see there two things which I need to take care of:

  1. Adding missing commas
  2. Create array from it.

To parse this response I'm using node JS. I've tried to convert this to array as follows:

const array = `[${response.data}.replace(/}/g, '},')}{}]`

Firstly I'm wrapping everything with array, add commas, and one empty object to get rid of last unwanted comma.

If I look at output everything seems ok, but when I'm trying to parse it with JSON.parse() I'm receiving

Unexpected ']' on position n

I'm not sure whether I'm doing something wrong or that data is corrupted since it is so long.

2
  • 4
    Fix what the server returns, that should already be valid JSON. Commented May 17, 2019 at 7:59
  • 1
    It looks like you may be building your JSON manually server-side, you probably should instead build a proper array and use JSON encoding function to export it Commented May 17, 2019 at 8:04

3 Answers 3

3

You can use a regular expression to add commas to the end of every line but the last, change someArray to "someArray", then surround the whole string in [] and JSON.parse it:

const response = `{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }`;

const json = '[' + response.replace(/}(?!$)/g, '},').replace(/someArray/g, '"$&"') + ']';
const arr = JSON.parse(json);
console.log(arr);

But this is still an X/Y problem - better to fix whatever's serving you that (effectively broken) data to get it to give you proper JSON instead, so that a hack like this isn't needed.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to omit the last comma and look ahead if some more characters are coming.

const array = `[${response.data}.replace(/}(?=.)/g, '},')}{}]`;
//                                         ^^^^^

Comments

0

If you really cannot modify the response of this list, you can use this:

const array = response.data.split(/\r?\n/).map(object => JSON.parse(object.replace("someArray", "\"$&\"")));

Each line will be split by split(/\r?\n/) and replace("someArray", "\"$&\"") will add the missing double quotes around someArray.

Working example:

const response = {
  "data": `{ "property1": "value", someArray: [123], "property2": [] }
           { "property1": "value", someArray: [123], "property2": [] }
           { "property1": "value", someArray: [123], "property2": [] }
           { "property1": "value", someArray: [123], "property2": [] }
           { "property1": "value", someArray: [123], "property2": [] }`
}

const array = response.data.split(/\r?\n/).map(line => JSON.parse(line.replace("someArray", "\"$&\"")));

console.log(array);

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.