Convert this:
{\"items\":[{\"id\":\"BLE89-A0-123-384\",\"weight\":\"100\",\"quantity\":3},
...
{\"id\":\"BLE10-A0-123-321\",\"weight\":\"
You can try with:
var obj = {
"items":[
{"id":"BLE89-A0-123-384","weight":"100","quantity":3},
{"id":"BLE10-A0-123-321","weight":"100","quantity":4}
],
"country":"JUS",
"region":"A",
"timeout":"FILLER"
};
var quantities = {};
obj.items.forEach(function (item) {
quantities[item.id] = item.quantity;
});
quantities
will then be the object {"BLE89-A0-123-384":3,"BLE10-A0-123-321":4}
. forEach is a native method of array objects in JavaScript that lets you iterate through their elements. You may want to put that piece of code inside a function:
function getQuantities(obj) {
var quantities = {};
obj.items.forEach(function (item) {
quantities[item.id] = item.quantity;
});
return quantities;
}
It helps if you don't think of JSON objects as JSON objects. Once you run a JSON string through JSON.parse, it is a native JavaScript object.
In JavaScript, there are two ways to access objects.
The dot notation goes like this
myObject.name
See the dot? You can use that to access any object property (which indeed may be another object in javascript, as long as it has a valid dot notation name). You can't use characters like -
, .
, and the space character.
myObject["variableName"]
Like dot notation but allows some other characters, like -
and the space character.. Does exactly the same thing.
Using these notations is useful because we can access nested properties.
myObj.foo.bar.baz()
Now let's get to your JSON object...
{"items":[{"id":"BLE89-A0-123-384","weight":"100","quantity":3,"stock":0},
{"id":"BLE10-A0-123-321","weight":"100","quantity":4,"stock":0}],
You might want to brush up on the JSON format yourself, but in your example, here's a few clues...
{
Means the start of an object. (Keep in mind your entire JSON string is an object itself.)
}
Means the end of an object.
"variable"
(with quotes! important in JSON, but not when accessing/declaring javascript objects) assigns a property to your object.
:
Is the assignment operator in both JSON and JavaScript objects.
Anything to the right of the :
is the value you are assigning to the property on the left.
,
Means you are starting a new property within an object.
You probably know that []
with ,
commas inside means an array.
When we run your string through JSON.parse(string)
, we'll get an object that looks like this...
var myResponse = JSON.parse(response);
You can now use it as a native JavaScript object. What you're looking for is a nested property within "items".
var items = myResponse.items; //alternatively you could just use myResponse.items
Since items
is an array of objects, we'll need to iterate through it in order to convert the existing object into a new object.
var i;
var result = {} ; //declare a new object.
for (i = 0; i < items.length; i++) {
var objectInResponse = items[i]; //get current object
var id = objectInResponse.id; //extract the id.
var quantity = objectInResponse.quantity;
result[id] = quantity; //use bracket notation to assign "BLE89-A0-123-384"
//instead of id. Bracket notation allows you to use the value
// of a variable for the property name.
Result is now an object that looks like:
{
"BLE89-A0-123-384" : 3, //additional properties designated by comma
"BLE10-A0-123-321" : 4 // The last key/value in an object literal CANNOT
// have a comma after it!
}
You can access the properties using bracket notation.
var BLE89 = result["BLE10-A0-123-321"]; //use quotes, otherwise JavaScript will try to look up the value of a variable.
You need to do the following:
var newJSON = {};
for (var i = 0; i < oldJSON.items.length; i++) {
newJSON[oldJSON.items[i].id] = oldJSON.items[i].quantity;
}