Reputation: 549
how to store JSON data into key-value pairs in javascript?
I have a json data file : data.js It contains follwing data
[
{ "Sound": "0.550951615" },
{ "Battery": 0.2567320563 },
{ "Screen": 0.0703125 },
{ "RAM": 0.1246117102 },
{ "Connectivity": 0 },
{ "Camera": 0.2721623766 },
{ "Design": 0.3677622768 },
{ "Processor": 0 }
]
how to store this data in key-value pair data-structure in java script?
Upvotes: 0
Views: 4732
Reputation: 1840
You need to process your data structure to get this done.
var a = [
{ "Sound": "0.550951615" },
{ "Battery": 0.2567320563 },
{ "Screen": 0.0703125 },
{ "RAM": 0.1246117102 },
{ "Connectivity": 0 },
{ "Camera": 0.2721623766 },
{ "Design": 0.3677622768 },
{ "Processor": 0 }
]
var keyValue = {}
a.forEach(function(e) {
keyValue[Object.keys(e)[0]] = Object.values(e)[0]
})
Output of console.log(keyValue)
{"Sound":"0.550951615","Battery":0.2567320563,"Screen":0.0703125,"RAM":0.1246117102,"Connectivity":0,"Camera":0.2721623766,"Design":0.3677622768,"Processor":0}
Upvotes: 1
Reputation: 232
JSON stands for JavaScript Object Notation. This is a systematic Syntax to store data.
for storing single value:
var Object1 = {
"Key": "Value"
}
for storing multiple value:
var Object1 = {
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3",
"nth key": "nth value"
}
Upvotes: 1
Reputation: 122936
You can use Array.map
or Array.reduce
(see MDN) to map the Array elements to an object, something like:
var objmap = {}, objred = {};
// parse the string (from your file)
var objFromFile =
JSON.parse('[\
{ "Sound": "0.550951615" },\
{ "Battery": 0.2567320563 },\
{ "Screen": 0.0703125 },\
{ "RAM": 0.1246117102 },\
{ "Connectivity": 0 },\
{ "Camera": 0.2721623766 },\
{ "Design": 0.3677622768 },\
{ "Processor": 0 }\
]');
// and map it to [obj]
var mapped = objFromFile.map(
function (v) { for (var l in v) {this[l] = v[l];} },
objmap
);
// or use reduce if you prefer it
var reduced = objFromFile.reduce(
function(p, n) {
for (var l in n) {p[l] = n[l];}
return p;
},
objred
);
// show the result, using JSON.stringify
document.querySelector('#result').textContent =
'mapped:\n' + JSON.stringify(objmap, null, ' ') +
'\n\nreduced:\n' + JSON.stringify(objred, null, ' ');
<pre id="result"></pre>
Upvotes: 1
Reputation: 190
You're already storing the data as key:value.
If you're wondering how you can access the various bits and pieces of your data, checkout this article: JavaScript loop through json array?
OR ...
You can find a particular k:v pair by index. In your case, if you store the payload in a variable, you could access the data like so:
var data = [
{ "Sound": "0.550951615" },
{ "Battery": 0.2567320563 },
{ "Screen": 0.0703125 },
{ "RAM": 0.1246117102 },
{ "Connectivity": 0 },
{ "Camera": 0.2721623766 },
{ "Design": 0.3677622768 },
{ "Processor": 0 }
]
data[0]
This would return { "Sound": "0.550951615" }
There are a TON of resources that you can search for JSON references.
Good Luck!
Upvotes: 0
Reputation: 8670
var myObject = {
key: value,
key: value
};
You would literally just list your contents in that format.
Other Resources: http://www.w3schools.com/js/js_objects.asp
How to "properly" create a custom object in JavaScript?
Upvotes: 0