Richard Cao Richard Cao - 3 months ago 14
Javascript Question

How to get JSON values with hyphens in the name of the values (javascript

So I have the following JSON retrieved from a website, and I was wondering how I would retrieve some data from it. Here is what the JSON data looks like.

{ name: '★ StatTrak™ Bayonet | Tiger Tooth (Factory New)',
price: 23763,
history:
{ '2016-06-14': { price: 31967, count: 0 },
'2016-06-15': { price: 33000, count: 0 },
'2016-06-16': { price: 32005, count: 0 },
'2016-06-17': { price: 30429, count: 0 },
'2016-06-18': { price: 27797, count: 0 },
'2016-06-19': { price: 28773, count: 0 },
'2016-06-20': { price: 29653, count: 0 },
'2016-06-21': { price: 30000, count: 0 },
'2016-06-22': { price: 31900, count: 0 },
'2016-06-23': { price: 27200, count: 0 },
'2016-06-24': { price: 29350, count: 0 },
'2016-06-26': { price: 33450, count: 0 },
'2016-06-27': { price: 29433, count: 0 },
'2016-06-28': { price: 28800, count: 0 },
'2016-06-29': { price: 31000, count: 0 },
'2016-06-30': { price: 30947, count: 0 },
'2016-07-01': { price: 30150, count: 0 },
'2016-07-03': { price: 31600, count: 0 },
'2016-07-04': { price: 24800, count: 0 },
'2016-07-05': { price: 34421, count: 0 },
'2016-07-06': { price: 29958, count: 0 },
'2016-07-07': { price: 29000, count: 0 },
'2016-07-08': { price: 29757, count: 0 },
'2016-07-10': { price: 30000, count: 0 },
'2016-07-11': { price: 34100, count: 0 },
'2016-07-12': { price: 33333, count: 0 },
'2016-07-13': { price: 26750, count: 0 },
'2016-07-14': { price: 25950, count: 0 },
'2016-07-15': { price: 26933, count: 0 },
'2016-07-16': { price: 26800, count: 0 },
'2016-07-17': { price: 26850, count: 0 },
'2016-07-18': { price: 29100, count: 0 },
'2016-07-19': { price: 28000, count: 0 },
'2016-07-20': { price: 27150, count: 0 },
'2016-07-21': { price: 25933, count: 0 },
'2016-07-23': { price: 26700, count: 0 },
'2016-07-24': { price: 26499, count: 0 },
'2016-07-25': { price: 26400, count: 0 },
'2016-07-26': { price: 22388, count: 0 },
'2016-07-27': { price: 22222, count: 0 },
'2016-07-28': { price: 22000, count: 0 },
'2016-07-29': { price: 23175, count: 0 },
'2016-07-30': { price: 23500, count: 0 },
'2016-07-31': { price: 23916, count: 0 },
'2016-08-01': { price: 25800, count: 0 },
'2016-08-02': { price: 25200, count: 0 },
'2016-08-04': { price: 22750, count: 0 },
'2016-08-07': { price: 20625, count: 0 },
'2016-08-08': { price: 24792, count: 0 },
'2016-08-09': { price: 21500, count: 0 }
}
}


I know you can access JSON data through . (periods) and so, I've tried doing body.history.2016-06-23.price , but then I get this error

C:\Users\Richard\Desktop\Steam\NodeJS\PriceCompareBot\test.js:11
console.log(body.history.2016-06-23.price);
^^^^^^^

SyntaxError: missing ) after argument list
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3


So I believe that it stops when it sees an integer? How do I get past this, and access the json price data of the dates? I'm sure there's a tiny thing that I'm forgetting, but I have no clue what it is. Thanks.

Answer

Accessing members with . is called dot notation. Accessing them with [] is called bracket notation.

The dot notation only works with property names which are valid identifier names, so basically any name that would also be a valid variable name and 2016-06-14 is not a valid variable name.

You can use body.history['2016-06-14'];