Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a json object that's inside a json object

Convert this:

{"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"}

To this:

{"BLE89-A0-123-384": "3", "BLE10-A0-123-321": "4"} that is... {id: quantity}

I found an answer that almost does what I need: Searching for an Object inside the JSON. But that answer doesn't help me because it's only at the first level (one json object). My problem is at the second level (json object within a json object). Thanks in advance!

like image 650
mrmo123 Avatar asked Apr 28 '12 22:04

mrmo123


1 Answers

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.

Dot Notation

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.

Bracket Notation (may be another name)

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.

like image 148
Stanley Stuart Avatar answered Sep 18 '22 15:09

Stanley Stuart