-5

I have the following string which I want to convert to JSON:

'{clientId: "1239268108.1505087088", userId: "0.4744496956388684", "url": "http://boomfix.es/", "pageUrl": "1", "timer": "15", "clickCount": "4", "mouseMax": "", "objective": ""}'

Why can't I use the JSON.parse method on this string?

Does every JSON element has to be in Quotes for JSON.parse to work?. So this would be different from the syntax on a Javascript object?

Rimo
  • 555
  • 1
  • 8
  • 18
  • 1
    Why can't you? What happens? – Sami Kuhmonen Sep 19 '17 at 18:16
  • 3
    It's not valid JSON. – trincot Sep 19 '17 at 18:16
  • https://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes – mikebridge Sep 19 '17 at 18:18
  • 2
    It's done with `JSON.parse('{"name": "value"}');` but with yours it doesn't work because it's invalid, are you sure you copy & pasted? – Toni Michel Caubet Sep 19 '17 at 18:18
  • @SamiKuhmonen if I run JSON.parse I get "Unexpected token c in JSON at position 1 at JSON.parse ()" – Rimo Sep 19 '17 at 18:20
  • @ToniMichelCaubet yeap, I copy pasting 100% – Rimo Sep 19 '17 at 18:21
  • Your attrs name must be "quoted", too – Toni Michel Caubet Sep 19 '17 at 18:21
  • @trincot What do you mean is not valid JSON? How do should I adjust it? Thank youuuuuu – Rimo Sep 19 '17 at 18:21
  • Could you show how you're doing the request? You usally can set the return value format – Toni Michel Caubet Sep 19 '17 at 18:22
  • In JSON property names must appear in double quotes. – trincot Sep 19 '17 at 18:24
  • @ToniMichelCaubet, for sure: gapi.client.analytics.data.realtime.get({ 'ids':'ga:' + profile_id, 'metrics':'rt:totalEvents', 'dimensions':'rt:eventAction,rt:eventLabel,rt:eventCategory', 'max_results':'25'}) .then(function(response) { var Object1 = response.result.rows[0][1]; – Rimo Sep 19 '17 at 18:27
  • @ToniMichelCaubet Hi Toni! I have corrected this this. Can you please revise it or give me advise on how to improve it to get an up vote? – Rimo Jan 19 '18 at 19:43
  • Hi @trincot! I have corrected this this. Can you please revise it or give me advise on how to improve it to get an up vote? – Rimo Jan 19 '18 at 19:43
  • You did well on improving the text of your question, but the baseline remains that the question does not show much of a research effort, as the documentation is *very* clear that property names in JSON must be quoted. A research would also have led you to JSON validators (freely available), which highlight exactly where a JSON is wrong. The lack of research is the main reason for getting downvotes (read the tooltip on the download button). – trincot Jan 19 '18 at 20:14
  • @trincot outside JSON.parse method I could use JSON before without doble quotes on the Key. That's why I got confused. Is the question now more useful for the community? is there a way you would improve to get upvotes I need to ask more questions? – Rimo Jan 19 '18 at 21:44
  • In my opinion this question cannot be turned in something useful. It has been asked before (like [here](https://stackoverflow.com/questions/20499831/why-is-json-parse-saying-invalid-character)), but shouldn't really, as people should look around a bit more before asking. My advice: don't look for upvotes. Instead, program, research, and then when stumbling on something you really cannot solve after looking really hard, then ask a well-formatted question about it, showing what you researched while trying to solve it (without success). – trincot Jan 19 '18 at 21:58
  • IF you really have proof that you could use a format without quotes around the keys with some product or service that claimed to produce/handle valid JSON, then you could try to focus the question on that, but it still wont bring me to upvote it, sorry. – trincot Jan 19 '18 at 22:01
  • @trincot I've edited it and I think it's a pretty helpful question now. I thought upvoting was about quality not eternal punishment hehe. Thank you, you rock anyway! – Rimo Jan 19 '18 at 22:11
  • 1
    The tooltip on the upvote button reads *"This question shows research effort; it is useful and clear"*. In my opion it fulfils the "clear" part, but not the other two for the reasons I have stated above. Just follow the advice I gave, and in the meanwhile you could also try to answer questions in areas that you master well. – trincot Jan 19 '18 at 22:34
  • @Rimo After this whole time, I would just let the question be.. try your best on future ones – Toni Michel Caubet Jan 22 '18 at 08:45
  • @ToniMichelCaubet You are right! But with this "-5" I can't either post nor answer questions. I'm stuck here 100%. – Rimo Jan 29 '18 at 19:11
  • ??? You have 34! – Toni Michel Caubet Jan 30 '18 at 07:08
  • Yeah, I need 50 points at least to answer questions & I can't post new ones having this -5 meanwhile. -5? Really. I think the community is a little bit too harsh. – Rimo Jan 30 '18 at 10:13

4 Answers4

1

Try wrap your field names in quotations - I found that worked for me at some stage

Greg Rebisz
  • 156
  • 7
1

If you put quotes around the identifiers, clientId and userId, it should work. I can parse it provided that is true.

https://jsfiddle.net/21d9qsgn/

var x = JSON.parse('{"clientId": "1239268108.1505087088", "userId": 
"0.4744496956388684", "url": "http://boomfix.es/", "pageUrl": "1", "timer": 
"15", "clickCount": "4", "mouseMax": "", "objective": ""}');
alert(x.clientId);
Sean Sherman
  • 327
  • 1
  • 16
1

As others have mentioned, your string isn't valid JSON. However, here's a hacky solution to make it valid:

var json_string = '{clientId: "1239268108.1505087088", userId: "0.4744496956388684", url: "http://boomfix.es/", pageUrl: "1", timer: "15", clickCount: "4", mouseMax: "", objective: ""}'

json_string = json_string.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9]+)(['"])?:/g, '$1"$3":')

var json = JSON.parse(json_string)

console.log(json.clientId)
Kodie Grantham
  • 1,963
  • 2
  • 17
  • 27
1

As we've mentioned in the comments, that's not a valid JSON format (the property names must be "quoted", too),

I think that you should adapt your request like so:

var query = gapi.client.analytics.data.realtime.get(({ 'ids':'ga:' + profile_id, 'metrics':'rt:totalEvents', 'dimensions':'rt:eventAction,rt:eventLabel,rt:eventCategory'‌​, 'max_results':'25'});
query.execute(function handleRTResponse(resultAsObject, resultAsJson) {
    console.log(resultAsJson); // this should be a valid JSON
});

Let me know how that works for you as I have not any environment with the gapi right now.

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378
  • 1
    It worked beautifully! I just applied JSON.parse right after inserting double quotes to each atribute and it works perfectly. Thank you for your help Toni! You are great! ;) – Rimo Sep 19 '17 at 18:49