I'm trying to get a URL from a nested JSON object, but am having trouble getting the data. I'm not sure how to get data out of a JSON object that's nested deeply.
app.controller('mainCtrl', function($scope, $http) {
$http.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D'www.whitehouse.gov%2Ffacts%2Fjson%2Fall%2Fcollege%2520affordability'&format=json&diagnostics=true").then(function(responce) {
$scope.status = responce;
$scope.sortData = responce.data.query.results.json.json.url;
console.log(responce);
console.log($scope.sortData);
});
});
I can console log the object, but I don't know how to get the URL from the deeply nested object. Here's the JSFiddle where the object is console logged. Thanks for the help.
Updated fiddle: https://jsfiddle.net/39pwve2x/23/
In the data, the URL is part of a repeating array object (the last "json" node). The URL is a property of each "json" in the array. If you need to get all of the URL's, pass the array to your view and ng-repeat over it:
<body ng-app="javascriptTest">
<div ng-controller="mainCtrl">
<div ng-repeat="item in sortData">
{{item.url}}
</div>
</div>
</body>
...and the controller...
var app = angular.module('javascriptTest', []);
app.controller('mainCtrl', function($scope, $http) {
$http.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D'www.whitehouse.gov%2Ffacts%2Fjson%2Fall%2Fcollege%2520affordability'&format=json&diagnostics=true").then(function(responce) {
$scope.sortData = responce.data.query.results.json.json;
});
});
You could also get a single URL by specifying an index. For example, if you only cared about the first URL returned by the data, you could do:
$scope.firstUrl = responce.data.query.results.json.json[0].url;
Short Answer
responce.data.query.results.json.json[0].url;
Long Answer
The responce.data.query.results.json.json is an array of objects, each object is identify by a index you need to provide what index you want to access.
You also can use a loop to get all the objects in the array
var data = responce.data.query.results.json.json
for (var i = 0; i < data.length; i++) {
console.log(data[i].url);
}