-1

I am having a code, which bulids file in the below format.

{"swap":1,"si":11},{"system":1,host:"new"},{"Cpu":1}

If I validate this file data i get an error as:

Parse error on line 4:

...": 1, "si": 11},{ "system": 1, ---------------------^ Expecting 'EOF'

How to resolve this issue?

joe
  • 21
  • 1
  • 5

4 Answers4

1

Wrap those Objects in to an JsonArray while building. Then in iterate through the array.

Raman Shrivastava
  • 2,923
  • 15
  • 26
1

In every key is double quoted "key". Your is missing double quotes at host key. Make sure you're writing a well-formed .

{ "system": 1, "host": "new" }
               ^    ^
1

am not a expert in JSON but i think you want to change a JSON like array value

[{"swap":1,"si":11},{"system":1,host:"new"},{"Cpu":1}]

insted of

{"swap":1,"si":11},{"system":1,host:"new"},{"Cpu":1}
Gowtham Sooryaraj
  • 3,639
  • 3
  • 13
  • 22
0

You can also use this custom function even if you have complex objects.

static getParsedJson(jsonString) {
    const parsedJsonArr = [];
    let tempStr = '';
    let isObjStartFound = false;
    for (let i = 0; i < jsonString.length; i += 1) {
        if (isObjStartFound) {
            tempStr += jsonString[i];
            if (jsonString[i] === '}') {
                try {
                    const obj = JSON.parse(tempStr);
                    parsedJsonArr.push(obj);
                    tempStr = '';
                    isObjStartFound = false;
                } catch (err) {
                    // console.log("not a valid JSON object");
                }
            }
        }
        if (!isObjStartFound && jsonString[i] === '{') {
            tempStr += jsonString[i];
            isObjStartFound = true;
        }
    }
    return parsedJsonArr;
}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
AJAY
  • 1
  • 4