0

I am trying to search through a json object to select some values. For example I have a variable with the value 'product-2' and I want to look through the json object and return the attributes array of 'product-2'

{

 "attributes": [     
...
 ],
"portfolio": [
{
    "conn": [
        {

                "product": "product-1",
                "description": "Description in here",
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],

        },
        {

                "product": "product-2",
                "description": "Description in here"
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }

    ]

}
]

Could anyone tell me how I can achieve this? Thank you

EDIT:

As per Pramods request - I was working with the following js (although its really wrong I am sure)

$scope.productAttributes = [];
    $scope.getProductDetails = function (product_id) {
        console.log(product_id);
        //search trough json
        angular.forEach($scope.listOfProducts.product_id, function(value, key) {

           // I was thinking I could loop through the json and when I find the matching product, then push its attributes into an array?
           // if (key === enteredValue) {
             //   $scope.productAttributes.push({atribute: key});
           // }
        });
    };

EDIT No.2

The JSON structure has changed

6
  • Please share what you have tried so far. Commented Feb 13, 2015 at 12:11
  • stackoverflow.com/questions/5288833/… Commented Feb 13, 2015 at 12:19
  • 1
    i think You have to use ` portfolio.conn[1].product-2.attributes ` use if work than ask me i will comment it because you have to accept these answer Commented Feb 13, 2015 at 12:26
  • You could try using JPath : s-anand.net/blog/jpath-xpath-for-javascript Commented Feb 13, 2015 at 12:32
  • Hey Gunjan - I tried that, Im already using allProducts.data.portfolio[0].conn to get to the product list. but I can't figure out how to search through conn for 'product-2' and loads its attributes into an array Commented Feb 13, 2015 at 14:31

3 Answers 3

1

Use a filter to destructure the array.

In my example I use a filter within a Controller. This should probably be done in a service or a view. For brevity I used the filter in a controller.

The filter expression essentially says, return the first object in the array with a property 'product' that is 'product-2'

var app = angular.module('app', []).controller('MyController', MyController);

MyController.$inject = ['$filter'];
function MyController($filter) {

  var data = [
        {

                "product": "product-1",
                "description": "Description in here",
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],

        },
        {

                "product": "product-2",
                "description": "Description in here",
                 "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }
    ]
  
    
    this.product = $filter('filter')(data, {product: "product-2"})[0];
  
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="MyController as vm">
    Product-2: {{vm.product}}
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Well, if you don't know how product-2 will be nested and you actually need to search for it, then you need to do a recursive search. A recursive function is a function that calls itself.

This means that you iterate over each of the keys, and if the key is an object, call the recursive function on that key as well, until the key you want is found.

Here is a similar question, with a few algorithms provided for doing recursive search on a JSON structure in JavaScript: traversing through JSON string to inner levels using recursive function

2 Comments

Thanks - I do know how the product-2 will be nested, as above. Does that mean I shouldn't do a recursive search?
The do as Gunjan Patel said in his comment.
0

I think your JSON is wrong , So please correct it

Correct JSON

{
    "attributes": [],
"portfolio": [
    {
    "conn": [
        {
            "product-1": {
                "label": "product-1",
                "description": "Description in here",
        "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }
        },
        {
            "product-2": {
                "label": "product-2",
                "description": "Description in here",
         "attributes": [
                    "OriginPostcode",
                    "Size",
                    "Bandwidth"
                ],
            }
        }
        ]
    }
]
}

And to parse above json and return product information following is the code

$(document).ready(function() {
    $.each(dict['portfolio'][0], function(key, list){
        $.each(list, function(index, value){
            $.each(value, function(product, info){
                if (product == "product-2"){
                    answer = {}
                    answer[product] = info;
                    return JSON.stringify(answer);
                }
            });
        });
    }); 
});

Fiddle link :-

http://fiddle.jshell.net/2t8uknkc/

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.