1

Starting to the following kind of string:

const json = '{"list":"[{"additionalInformation": {"source": "5f645d7d94-c6ktd"}, "alarmName": "data", "description": "Validation Error. Fetching info has been skipped.", "eventTime": "2020-01-27T14:42:44.143200 UTC", "expires": 2784, "faultyResource": "Data", "name": "prisco", "severity": "Major"}]"}'

How can I manage this as a JSON? The following approach doesn't work

const obj = JSON.parse(json );
unuspected result

How can I parse it correctly?

In conclusion, I should extract the part relative to the first item list and then parse the JSON that it contains.

6
  • 1
    could you add information on what you are expecting because your code seams fine. Commented Jan 27, 2020 at 16:07
  • I would like to extract the json, should I remove []? Commented Jan 27, 2020 at 16:11
  • Well it depends, You json is an array, if you don't want to keep the array, you could retrieve the first element. JSON.parse('[{"result":true, "count":42}]')[0]. Commented Jan 27, 2020 at 16:12
  • 2
    It is already a valid JSON string. "doesn't work" how? Commented Jan 27, 2020 at 16:12
  • json string updated Commented Jan 27, 2020 at 16:20

2 Answers 2

3

Your JSON is invalid. The following is the valid version of your JSON:

const json= {
    "list": [ {
        "additionalInformation": {
            "source": "5f645d7d94-c6ktd"
        },
        "alarmName": "data",
        "description": "Validation Error. Fetching info has been skipped.",
        "eventTime": "2020-01-27T14:42:44.143200 UTC",
        "expires": 2784,
        "faultyResource": "Data",
        "name": "prisco",
        "severity": "Major"
      }
    ]
}

The above is already a JSON and parsing it as JSON again throws an error.

JSON.parse() parse string/ text and turn it into JavaScript object. The string/ text should be in a JSON format or it will throw an error.

Update: Create a function to clean your string and prepare it for JSON.parse():

cleanString(str) {
    str = str.replace('"[', '[');
    str = str.replace(']"', ']');
  return str;
}

And use it like:

json = this.cleanString(json);
console.log(JSON.parse(json));

Demo:

let json = '{"list":"[{"additionalInformation": {"source": "5f645d7d94-c6ktd"}, "alarmName": "data", "description": "Validation Error. Fetching info has been skipped.", "eventTime": "2020-01-27T14:42:44.143200 UTC", "expires": 2784, "faultyResource": "Data", "name": "prisco", "severity": "Major"}]"}';
json = cleanString(json);
console.log(JSON.parse(json));

function cleanString(str) {
	str = str.replace('"[', '[');
	str = str.replace(']"', ']');
  return str;
}

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

1 Comment

is there any way to take the object by doing something like : json['list']
0

Remove the double quotes from around the array brackets to make the json valid:

const json = '{"list":[{"additionalInformation": {"source": "5f645d7d94-c6ktd"}, "alarmName": "data", "description": "Validation Error. Fetching info has been skipped.", "eventTime": "2020-01-27T14:42:44.143200 UTC", "expires": 2784, "faultyResource": "Data", "name": "prisco", "severity": "Major"}]}'

1 Comment

Demo in answer from Maihan

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.