0

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?

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
vinaykp
  • 539
  • 2
  • 5
  • 16

5 Answers5

1

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>
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • If I saw an `Array.map` in a section of code I'd still be expecting an array of values back - `Array.reduce` would be cleaner (using the second parameter). – Emissary Apr 24 '15 at 15:44
  • @Digigizmo: have you actually tried using `reduce`? – KooiInc Apr 24 '15 at 15:49
1

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"
}
adiga
  • 34,372
  • 9
  • 61
  • 83
Udal Pal
  • 222
  • 1
  • 9
1

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}
Cybersupernova
  • 1,833
  • 1
  • 20
  • 37
0
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?

Community
  • 1
  • 1
zfrisch
  • 8,474
  • 1
  • 22
  • 34
0

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!

Community
  • 1
  • 1
ThaDick
  • 193
  • 2
  • 11
  • thanks, actualy I want to access each filed in the json data ,hw to do that? – vinaykp Apr 24 '15 at 16:05
  • The link I mentioned in the answer has several examples of how to loop through the array of data ... I'm not quite sure if I understand what you're asking for, if that post doesn't answer your question. – ThaDick Apr 27 '15 at 13:01