-1

My string looks like this one below. But while converting it to JSON by javascript/jQuery the error is received "Invalid Charater". Please help

'[{ color: "#8B0000",  data: [{ "y":  12000,  "x":  0}],  pointRange: 8,  type: "column",  name: "Segment1"}, { color: "#FFA500",  data: [{ "y":  11000,  "x":  10}],  pointRange: 12,  type: "column",  name: "Segment2"}, { color: "#11ddbb",  data: [{ "y":  10000,  "x":  19}],  pointRange: 6,  type: "column",  name: "Segment3"}, { color: "#8B0000",  data: [{ "y":  8000,  "x":  24}],  pointRange: 5,  type: "column",  name: "Segment4"}]'

This is the code I am using to parse it to JSON.

var response ='[{ color: "#8B0000",  data: [{ "y":  12000,  "x":  0}],  pointRange: 8,  type: "column",  name: "Segment1"}, { color: "#FFA500",  data: [{ "y":  11000,  "x":  10}],  pointRange: 12,  type: "column",  name: "Segment2"}, { color: "#11ddbb",  data: [{ "y":  10000,  "x":  19}],  pointRange: 6,  type: "column",  name: "Segment3"}, { color: "#8B0000",  data: [{ "y":  8000,  "x":  24}],  pointRange: 5,  type: "column",  name: "Segment4"}]';
response=$.parseJSON(response);
alert(response);
Monsta
  • 59
  • 3
  • 7
  • 2
    What you have is not valid JSON. See http://json.org/ or https://en.wikipedia.org/wiki/JSON for the correct syntax. – Felix Kling Nov 03 '16 at 06:20
  • You can use jsonlint.com to validate your JSON. Your one, as mentionted above, is invalid. It isn't JSON, it is a JavaScript object literal. You *can* actually `eval(response)`, but you definitely **do not want to**. – Yeldar Kurmangaliyev Nov 03 '16 at 06:25
  • @FelixKling I have restriction here. I am using highcharts and the JSON keys and int values can not be inside the double quotes. Please suggest. – Monsta Nov 03 '16 at 06:25
  • Add double quotes to all your keys and it should be fine. – Oluwafemi Nov 03 '16 at 06:30
  • *"JSON keys [...] can not be inside the double quotes"* Once you have an object, it is irrelevant how it was created. It's seems you are confusing a couple of things, especially JSON, a data format, with JavaScript objects. Unfortunately you are not providing much information about your actual issue. The only thing we can tell you is that the string doesn't contain valid JSON. Maybe you have to fix it to have valid JSON. Maybe you don't need a string at all and can just write down the array of objects. – Felix Kling Nov 03 '16 at 06:43

5 Answers5

0

The original reponse message is an invalid json.

Option 1 :

What you have is a Javascript Object (provided you remove those single quotes marks). You don't have a JSON String but Javascript Object.

After removing quotes from the start, it will look like

var response = [{ color: "#8B0000",  data: [{ "y":  12000,  "x":  0}],  pointRange: 8,  type: "column",  name: "Segment1"}, { color: "#FFA500",  data: [{ "y":  11000,  "x":  10}],  pointRange: 12,  type: "column",  name: "Segment2"}, { color: "#11ddbb",  data: [{ "y":  10000,  "x":  19}],  pointRange: 6,  type: "column",  name: "Segment3"}, { color: "#8B0000",  data: [{ "y":  8000,  "x":  24}],  pointRange: 5,  type: "column",  name: "Segment4"}];

var jsonString = JSON.stringify(response);

You now have option of converting it to JSON string using JSON.stringify().

You also have an option 2 :

Add double quotes to all the key values, that will also work. That would make it a JSON string. You can then use JSON.parse() to convert it to JSON Object.

var response = '[{ "color": "#8B0000", "data": [{ "y":12000, "x":0}], "pointRange": 8, "type": "column", "name": "Segment1"}, { "color": "#FFA500", "data": [{ "y": 11000, "x": 10}], "pointRange": 12, "type": "column", "name": "Segment2"}, { "color": "#11ddbb", "data": [{ "y": 10000, "x": 19}], "pointRange": 6, "type": "column", "name": "Segment3"}, { "color": "#8B0000", "data": [{ "y": 8000, "x": 24}], "pointRange": 5, "type": "column", "name": "Segment4"}]';

var jsonObject = JSON.parse(response);
Akshay Soam
  • 1,580
  • 3
  • 21
  • 39
0

If you have time please visit http://www.json.org/. There you will find the specification of JSON.

One of your problems is: the key in the object must be a string in double quotes.

For example: '{ color: "#8B0000" }' is wrong

It must be: '{ "color": "#8B0000" }'

vothaison
  • 1,646
  • 15
  • 15
0

the below may be a solution

var response ='[{ color: "#8B0000",  data: [{ "y":  12000,  "x":  0}],  pointRange: 8,  type: "column",  name: "Segment1"}, { color: "#FFA500",  data: [{ "y":  11000,  "x":  10}],  pointRange: 12,  type: "column",  name: "Segment2"}, { color: "#11ddbb",  data: [{ "y":  10000,  "x":  19}],  pointRange: 6,  type: "column",  name: "Segment3"}, { color: "#8B0000",  data: [{ "y":  8000,  "x":  24}],  pointRange: 5,  type: "column",  name: "Segment4"}]';
var Obj=eval(response);
var response=JSON.stringify(Obj);
alert(response);
ohsang2018
  • 11
  • 3
  • Eh, why are you converting the array back to JSON? – Felix Kling Nov 03 '16 at 06:59
  • the string you use is not a standard json string,but can be eval to a javascript object,then use JSON.stringify to convert it to standard json string .if need javascript object,you only need to eval the string – ohsang2018 Nov 03 '16 at 07:07
0

In Javascript json string Convert all keys into quoted string like color to "color" etc.

var response = '[{ "color": "#8B0000",  "data": { "y":  12000,  "x":  0},  "pointRange": 8,  "type": "column",  "name": "Segment1"}, { "color": "#FFA500",  "data": [{ "y":  11000,  "x":  10}],  "pointRange": 12,  "type": "column",  "name": "Segment2"}, { "color": "#11ddbb",  "data": [{ "y":  10000,  "x":  19}],  "pointRange": 6,  "type": "column",  "name": "Segment3"}, { "color": "#8B0000",  "data": [{ "y":  8000,  "x":  24}],  "pointRange": 5,  "type": "column",  "name": "Segment4"}]';
response=JSON.parse(response);
console.log(response);
0

@Monsta - you are missing double quote in the key; the code should be like this:

var response ='[{ "color": "#8B0000",  "data": [{ "y":12000,  "x":0}],  "pointRange": 8,  "type": "column",  "name": "Segment1"}, { "color": "#FFA500",  "data": [{ "y":  11000,  "x":  10}],  "pointRange": 12,  "type": "column",  "name": "Segment2"}, { "color": "#11ddbb",  "data": [{ "y":  10000,  "x":  19}],  "pointRange": 6,  "type": "column",  "name": "Segment3"}, { "color": "#8B0000",  "data": [{ "y":  8000,  "x":  24}],  "pointRange": 5,  "type": "column",  "name": "Segment4"}]';
JSON.parse(response);
Alexandru Marculescu
  • 5,569
  • 6
  • 34
  • 50