0

I have a JSON string that looks like this:

[
    {
        "id": "acbPreviewCell",
        "width": 80
    },
    {
        "id": "advName",
        "width": 170
    },
    {
        "id": "adHeadline",
        "width": 150
    },
    {
        "id": "adSize",
        "width": 150
    },
    {
        "id": "adProduct",
        "width": 150
    },
    {
        "id": "adCategory",
        "width": 150
    },
    {
        "id": "adSection",
        "width": 150
    },
    {
        "id": "adColor",
        "width": 150
    },
    {
        "id": "adTags",
        "width": 150
    },
    {
        "id": "adRegions",
        "width": 150
    },
    {
        "id": "adStatus",
        "width": 150
    },
    {
        "id": "adCreated",
        "width": 150
    },
    {
        "id": "adBookingNb",
        "width": 150
    },
    {
        "id": "adPickup",
        "width": 150
    },
    {
        "id": "folioMeta",
        "width": 150
    }
]

To help explain further, each of these entries is the ID of a table header, along with the width of that table header. I'm using it within an application so that I can remember a user's custom column width that they set.

I would like to break this JSON string down into a javascript array, so that I can easily access each ID and it's width.

Any help would be appreciated with setting it up as an array and then breaking it down to access the ID and the width. Thank you!

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
jmchauv
  • 147
  • 17

6 Answers6

2

If you want to convert it to an array you can use the built-in JSON object to parse it: JSON.parse(yourString).

But if you want to easily access values by ID, you will have to actually convert it to an object:

var originalData = JSON.parse(yourString);
var parsedData = {};
for (var i = 0, l = originalData.length; i < l; i++) {
    parsedData[originalData[i].id] = originalData[i].width;
}

// now you can easily access the wanted widths
var acbPreviewCellWidth = parsedData['acbPreviewCellWidth'];
1

Someone else mentioned a hashed map, which is pretty much what I'm guessing you need. That is to say, you want to access by id quickly as in array['advName'] gives you the width there.

You could mill through the values you get like so:

a = [
    {
        "id": "acbPreviewCell",
        "width": 80
    },
    {
        "id": "advName",
        "width": 170
    },
    {
        "id": "adHeadline",
        "width": 150
    }
];

function rewrite_array(arr){
    new_arr = new Array();
    for( i=0; i<a.length; i++){
        new_arr[arr[i]['id']] = arr[i]['width'];
    }
    return new_arr;
}

n = rewrite_array(a);
console.log(n["advName"]);
console.log(a[1]['width']);
Camilo Payan
  • 181
  • 7
0

In theory, it already is stored as an array. However, if you would like to parse the JSON object to ensure that it is properly formed, use var jsonArray = JSON.parse(jsonString); No jQuery is needed.

Peter Maidens
  • 443
  • 1
  • 6
  • 11
0

Use jQuery.parseJSON(jsonString)

Mihir
  • 419
  • 3
  • 10
0

You might want to get the JSON using ajax, and then loop through all the values lushing them to an array in javascript, or you can simply call var objArr = $.parseJSON('file'); to get an array of all the objects. If you want to fet the values, you van simply get them using objArr[index].val.

Using this you can call, for instance, the first objects values like this.

var id = objArr[0].id;
var width = objArr[0].width;
Max Langerak
  • 1,187
  • 8
  • 17
0

Try

function filtered(v) {
  return $.map(json, function(value) {
    return value.id === v ? value : null
  })[0];
};

usage

filtered("folioMeta"); // do stuff with "folioMeta" object within array
filtered("folioMeta").id // `"folioMeta"`
filtered("folioMeta").width // `150`
guest271314
  • 1
  • 15
  • 104
  • 177