I am trying to create a JSON object in Node.js without any success. For example an object like this:
{ 'Orientation Sensor':
[ { sampleTime: '1450632410296',
data: '76.36731:3.4651554:0.5665419' },
{ sampleTime: '1450632410296',
data: '78.15431:0.5247617:-0.20050584' } ],
'Screen Orientation Sensor':
[ { sampleTime: '1450632410296',
data: '255.0:-1.0:0.0' } ],
'MPU6500 Gyroscope sensor UnCalibrated':
[ { sampleTime: '1450632410296',
data: '-0.05006743:-0.013848438:-0.0063915867},
{ sampleTime: '1450632410296',
data: '-0.051132694:-0.0127831735:-0.003325345'}]}
but in a dynamic way without any knowledge about the size of every item. Is there something like that in Node.js?
This question is related to
javascript
node.js
json
The JavaScript Object()
constructor makes an Object that you can assign members to.
myObj = new Object()
myObj.key = value;
myObj[key2] = value2; // Alternative
The other answers are helpful, but the JSON in your question isn't valid. I have formatted it to make it clearer below, note the missing single quote on line 24.
1 {
2 'Orientation Sensor':
3 [
4 {
5 sampleTime: '1450632410296',
6 data: '76.36731:3.4651554:0.5665419'
7 },
8 {
9 sampleTime: '1450632410296',
10 data: '78.15431:0.5247617:-0.20050584'
11 }
12 ],
13 'Screen Orientation Sensor':
14 [
15 {
16 sampleTime: '1450632410296',
17 data: '255.0:-1.0:0.0'
18 }
19 ],
20 'MPU6500 Gyroscope sensor UnCalibrated':
21 [
22 {
23 sampleTime: '1450632410296',
24 data: '-0.05006743:-0.013848438:-0.0063915867
25 },
26 {
27 sampleTime: '1450632410296',
28 data: '-0.051132694:-0.0127831735:-0.003325345'
29 }
30 ]
31 }
There are a lot of great articles on how to manipulate objects in Javascript (whether using Node JS or a browser). I suggest here is a good place to start: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Source: Stackoverflow.com