-1

var result = "";
result += "[";
for(i=0;i<=10;i++)
  {
    result += "{ 'key': 'keyvalue" + i + "', 'values': [";
    for(j=0;j<=10;j++)
      {
        result += "{ 'key': 'subkeyvalue"+j+"', 'value':"+j+"}, ";
      }
    result += "]}, ";
  }

result += "]";

console.log(result);
console.log(JSON.stringify(result));
console.log(JSON.parse(result));

If i try to convert String to JSON.parse. I am getting below error.

JSON.parse: expected property name or '}' at line 1 column 4 of the JSON data

can you please any one resolve this problem.

10
  • 5
    Wrap keys and values in double quotes Commented Oct 19, 2016 at 4:52
  • 1
    BTW why you are doing this? As you can do the same with JSON.stringify passing JavaScript object; Commented Oct 19, 2016 at 4:55
  • why you are creating string ? make proper object and add to array and then use JSON.stringify() to make json string Commented Oct 19, 2016 at 4:56
  • @Tushar said Wrap keys and values in double quotes not in single; Commented Oct 19, 2016 at 4:58
  • after converting string to JSON object i need to create chart Commented Oct 19, 2016 at 4:58

4 Answers 4

1

In this snippet ( result += "]}, ";) , "," (comma) is getting appended at the last, so the json will be like "},]" where you will be expecting like "}]"

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

3 Comments

You should add this in comment as this is not a complete answer;
May be result += '{ "key": "subkeyvalue'+j+'", "value":'+j+'}, '; then is this statement is correct?
based on my requirement result is coming. but its coming string format
0

Don't create JSON strings manually through string concatenation. Just...don't.

Create an array, create appropriate objects and add them to the array, then pass the array to your charting library as is.

Or if you actually need a JSON string use JSON.stringify() on the array.

And note that there is no such thing as a "JSON object".

var result = [];
var vals;
for(var i=0;i<=10;i++) {
  vals = [];
  for(j=0;j<=10;j++) {
    vals.push( { key: 'subkeyvalue'+j, value: j } );
  }
  result.push( { key: 'keyvalue' + i, values: vals } );
}

// someChartFunction(result);

console.log(result);
console.log(JSON.stringify(result));

Comments

0

I think you can use JSON.stringify() or use key and value in (') quote

var result = "";
result += "[";
for(i=0;i<=10;i++)
  {
    result += "{ 'key': 'keyvalue" + i + "', 'values': [";
    for(j=0;j<=10;j++)
      {
        result += "{ 'key': 'subkeyvalue"+j+"', 'value':"+j+"}, ";
      }
    result += "]}, ";
  }

result += "]";

console.log(result);

console.log(JSON.stringify(result));

1 Comment

i tried JsON.stringify again it showing same string format result
0

Try to use the code given below. You should not append the , (Comma) to the last element while iterating

var result = "";
result += "[";
for(i=0;i<=10;i++)
  {
    result += '{ "key": "keyvalue' + i + '", "values": [';
    for(j=0;j<=10;j++)
      {
        result += '{ "key": "subkeyvalue'+j+'", "value":'+j+'}';
        if(j!=10) {
          result += ','
        }
      }
    result += ']} ';
    if(i!=10) {
    	result += ','
    }
  }

result += ']';

console.log(result);

console.log(JSON.parse(result));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.