0

I have this JSON that I'm getting back from a web service call:

{
    "name": "My Name",
    "path": "my path",
    "id": "44",
    "type": "my type",
    "classes": "my classes"
},
{
    "name": "his Name",
    "path": "his path",
    "id": "76",
    "type": "his type",
    "classes": "his classes"
}

I then need to convert it to this format

{
    "44" : { "name" : "My Name", "path" : "my path" },
    "76" : { "name" : "his Name", "path" : "his path" }
}

My initial naive attempt was this:

var myData = [];
for (var i = 0; i < rawData.length; i++) {
myData.push({
    rawData[i].id :
    {
        "path": rawData[i].path,
        "name": rawData[i].name
    }
});

which fails with syntax errors, so I eventually got to this:

var myData = [];
for (var i = 0; i < rawData.length; i++) {
myData.push(rawData[i].id,
{
    "path": rawData[i].path,
    "name": rawData[i].name
});

and it mostly works. My array is populated, but the problem is that my myData array doesn't have the "44", and "76" part of the object, just the { "name" : "", "path" : "" } part. I expect this is due to a lack of understanding on my part of how JSON and javscript objects work.

1
  • This has nothing to do with JSON. Those are arrays and objects, not text. Commented Apr 9, 2014 at 16:50

4 Answers 4

1

Your desired output isn't an array, so that's your starting point. The output you've said you want is an object, not an array.

You build your result by creating a blank object and then adding the objects to it using id as the key:

var myData = {};
rawData.forEach(function(entry) {
    myData[entry.id] = {
        name: entry.name,
        path: entry.path
    };
});

Or if you don't want to use forEach (it's ES5, but can be shimmed for older browsers), the old-fashioned way:

var myData = {};
var index, entry;
for (index = 0; index < rawData.length; ++index) {
    entry = rawData[index];
    myData[entry.id] = {
        name: entry.name,
        path: entry.path
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was missing, after making the leap from array to object it makes perfect sense. Thanks.
1

Don't use Array.prototype.push(), use the square bracket notation and define your output as an object not an array.

var myData = {};
for (var i = 0; i < rawData.length; i++) {
    myData[rawData[i].id] = {
        "path": rawData[i].path,    
        "name": rawData[i].name
    }
}

Comments

0

You need to convert your id to a string?

var myData = {};
for (var i = 0; i < rawData.length; i++) {
    myData[String(rawData[i].id)] = {
        "path": rawData[i].path,
        "name": rawData[i].name
    };
}

1 Comment

"You need to convert your id to a string?" No, it's already a string. And if it weren't, it would become one as soon as you used it as a property name ([...] notation).
0

A variation on what other posters have written:

// Create a new empty object.
var out = {};

// Loop over your array of objects
// Add the each object id as a key to the output object, and the object as the value.
for (var i = 0, l = arr.length; i <l; i++) {
  var obj = arr[i];
  out[obj.id] = obj;

  // Delete the properties from the newly added object you don't want.
  delete obj.id;
  delete obj.type;
  delete obj.classes;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.