3

I want to convert a JSON response to associative array but looks like I was not able to make it right.

Here is my JSON sample response:

{
   "response":[
  {
     "id":100,
     "certificate":{
        "id":1,
        "title":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."

     },
     "created_at":"2013-12-02T15:20:08.233Z"

  },
  {
     "id":101,
     "certificate":{
        "id":2,
        "title":"Aenean facilisis, nisl vitae pulvinar varius."
     },
     "created_at":"2013-12-02T15:20:08.240Z"

  }
 ],
"count":2
}

This is what I have tried so far:

    var len = obj.response.length;
    var rData = [];
    var gcData = [];


    for(var i = 0; i < len; i++){

        rData[i] = $.map(obj.response[i], function(value, index) {

            if(typeof value=="object"){
                gcData = $.map(value, function(value1, index) {
                    return [value1];
                });

                return gcData;

            }else{              
                return [value];
            }
        });     

    }

My expected output:

rData = [
    100 : [
           id: ["100"],
           certificate: [
              id: ["1"],
              title: ["Lorem ipsum dolor sit amet, consectetur adipiscing elit."]
           ],
           created_at: ["2013-12-02T15:20:08.240Z"]   

         ]
    101 : [
           id: ["101"],
           certificate: [
              id: ["2"],
              title: ["Aenean facilisis, nisl vitae pulvinar varius."]
           ],
           created_at: ["2013-12-02T15:20:08.240Z"]   

         ]

]

Please help. Thanks.

5
  • 1
    That part rData = [ 100 : .. is already invalid. And why would you want to put every single value in an array? That makes accessing the data so much more difficult. JavaScript doesn't have associative arrays, but you can use objects as key -> value map. What's wrong with the existing data structure, i.e. the way how the JSON is structured? Do you just want to create a map id -> object? Please explain the problem you are actually trying to solve. Commented Dec 5, 2013 at 9:25
  • @FelixKling Okay, I just want to group it by keys something like I have in expected output. Though not necessarily the same. I have difficulty especially on inner part "certificate". Commented Dec 5, 2013 at 9:30
  • Also I wanted to point out that your problem doesn't seem to have anything to do with JSON. The value you are processing, obj.response, is a JavaScript array. How you obtained the data (i.e. as JSON) is irrelevant to what you are trying to do. Commented Dec 5, 2013 at 9:42
  • @FelixKling I am making things complicated here where in I can use directly the obj.response instead of converting it into an array. I'll do some experiments on my end and will accept answer later on. Thanks for the help. Commented Dec 5, 2013 at 9:47
  • Yes, if you don't need an id -> object mapping, directly using obj.response is of course the easiest approach. Commented Dec 5, 2013 at 9:50

2 Answers 2

6

I just want to group it by keys something like I have in expected output

It seems you want to create an id -> object map. To do that you just have to iterate over the array, take the id attribute of each object as property name and assign the object (the element of the array) to that property of the map.

Example:

var map = {};
var response = obj.response;

for (var i = 0, l = response.length; i < l; i++) {
    map[response[i].id] = response[i];
}

console.log(map);

Each object inside the array is already in the structure you want it to be. The output is

{
    "100": {
        "id": 100,
        "certificate": {
            "id": 1,
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        },
        "created_at": "2013-12-02T15:20:08.233Z"
    },
    "101": {
        "id": 101,
        "certificate": {
            "id": 2,
            "title": "Aenean facilisis, nisl vitae pulvinar varius."
        },
        "created_at": "2013-12-02T15:20:08.240Z" 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

your desired JSON is incorrect.

It isn't clear if you want to create an array of arrays or an object of arrays or what. To help decide which option fits your need, try this code

var a=[];
var o={};

for (var i=0, l=response.length; i<l; i++) {
  var e = response[i];
  a[e.id] = e.certificate;
  o[e.id] = e.certificate;
}

console.log(JSON.stringify(a));
console.log(JSON.stringify(o));

2 Comments

Did you have a look at the output of console.log(JSON.stringify(a));?
yes, I know it's not what the OP requested, but that's my point. The output of o isn't neither.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.