Search code examples
node.jsjsonnode-sqlite3

Key not being recognized in JSON


I have following JSON that i am trying to parse.

{"id":1,"colour":"blue","count(colour)":1}

It is a result of what i have returned from my sqlite3 select staement. I am doing a count(colours), which is being returned as the key in the JSON. Then when i attempt to reference the value using .count(colour), my node app fails giving me an error that colour is not defined. Note, that referencing .id works just fine.

Has anybody ran into this issue before or can provide any help?


Solution

  • If your JSON is in some variable, myjson, and you access it directly with myjson.count(colours), you get the error because it's trying to execute a function in your object.

    It works as you expect if you access via string like this: myjson["count(colour)"].