Problem extract data file json from Imgur API - javascript

I have a problem to extract a data from a json file created from a GET from Imgur API.
That is the json :
{
"id": "ADgvagy",
"title": "This game is so much fun ",
...
"include_album_ads": false,
"images": [
{
"id": "VLeeDfj",
...
"in_gallery": false,
"link": "https://i.imgur.com/VLeeDfj.mp4",
"mp4_size": 2707912,
"mp4": "https://i.imgur.com/VLeeDfj.mp4",
"gifv": "https://i.imgur.com/VLeeDfj.gifv",
"hls": "https://i.imgur.com/VLeeDfj.m3u8",
"processing": {
"status": "completed"
},
"comment_count": null,
...
"score": null
}
]
},
I have no problem to extract the title data when I do this.props.post.title.
I tried :
this.props.post.images.link -> TypeError: undefined is not an object (evaluating 'this.props.post.images.link')
this.props.post.images[0].link -> TypeError: undefined is not an object (evaluating 'this.props.post.images[0]')
Thanks for your time.

I found it. The correct way is to do this.props.post["images"][0].link

Related

How to get a particular data from JSON string coming from API in ANGULAR 8 and store in Variable [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 2 years ago.
Good day Developers in the house. I need help. I have a json string that i have gotten from API in Angular and console log to see it. I want to get a particular data from the json file and store in a variable because i need to query the data again. Below is how my Json data look like.
{
"done": true,
"record_id": "5fc0a7ac88d2f8534f8e59d8",
"customer_id": "5fa1541f6bd09b290f736608",
"balance": {
"clientId": "qh8RKp9BGhmKCn8ZAAED",
"status": true,
"balance_callback": "https://webhook.site/92b09e29-f08e-4472-b7e8-5875155360d67",
"data": {
"formatted": [
{
"available_balance": 31500,
"ledger_balance": 32000,
"ref": "saving-account",
"status": "active",
"account": "5fa9e536f6b7bb837cb22byu",
"connected": true
},
{
"available_balance": 11200,
"ledger_balance": 11200,
"ref": "current-account",
"status": "active",
"account": "5fa9e535f6b7bb837cb22buy",
"connected": false
},
{
"available_balance": 2000,
"ledger_balance": 2000,
"ref": "current-account",
"status": "active",
"account": "5fa9e536f6b7bb837cb22bty",
"connected": false
}
]
}
},
"Post": {
"callback_url": "https://webhook.site/92b09e29-f08e-4472-b7e8-5875155360d67"
},
"guarantors": [],
"redirect_url": "",
"launchAgain": true,
"hideExit": "",
"options": {},
"directors": null,
"auth": {
"clientId": "qh8RKp9BGhmKCn8ZAythj",
"status": true,
}
I want to get the callback-url under Post in the json file above.. Please any idea on how to do that with any javascript Method.
Let's say you call the date like that
fetch('urlOfTheApi')
.then( response => resonse.json()
.then( json => {
console.log(json);
});
So the solution is
fetch('urlOfTheApi')
.then( response => resonse.json()
.then( json => {
let callback_url = json.POST.callback;
return fetch(callback_url, {method:'POST'}).
})
.then( () => console.log('ok')
.catch( console.error );
Based on your JSON you should create an interface to define the object of the JSON and the particular part of the JSON that you need if you just need that property you could create the following interface
interface Response {
Post: {
callback_url: string;
}
}
In the service where you are requesting the endpoint from your API, you set that this method returns a Response type of data.
And inside of the subscription on your method you could access the property of the object of the JSON.

Error getting data from Kendo UI dataset

dsDetails = new kendo.data.DataSource({
transport: {
read: {
url: PRODUCTDETAILAPI,
dataType: "json",
data: { "site_key": SITEKEY, "id": id }
}
}
});
dsDetails.fetch(function() {
var myData = dsDetails.data();
console.log(myData);
// bind stuff here
});
The API call is returning the following data (so I know that is working):
{
"main_category": "Gifts",
"expires_on": null,
"html_address": "678 Sterling Drive\u003Cbr/\u003ESuite 102\u003Cbr/\u003ESanford FL 32771",
"address": "678 Sterling Drive Suite 102, Sanford Florida 32771",
"status": "Visible",
"rating": 5, "sub_category": "Gift Baskets",
"quantity": 92, "categories": ["Gifts: Gift Baskets"], "title": "Luggage Duffel",
"price": 12210, "images": [], "short_address": "Sanford FL",
"short_title": "Luggage Duffel",
"posted_at": "2011/09/23",
"category": "Gifts: Gift Baskets",
"id": "product_listing_88",
"price_note": "",
"description": "Black, 60 in long",
"seller": "Morning Star Media Group"
}
When I run this code I get the following error:
Uncaught TypeError: Object #<Object> has no method 'slice'
Edit: Based on the selected answer, I replace this kendo dataset code with a standard jQuery .ajax call and it worked fine. My brain was temporarily stuck within the confines of the Kendo framework.
The dataSource is used to get collection of objects from the server. This exception is thrown when the item returned from the server is not the collection. Instead it is an object which has a field that actually holds the array.
If the dataset (array) that you want to use is one of the fields in the returned object, you should use dataSource.schema.data configuration to specify that field, so the dataSource can understand in which field from the response to search the collection.

javascript retrieve value from json

How to get value from [object Object] in javaScript.
i have a json response from php which i pass in javascript .
i want GPSPoint_lat,GPSPoint_lon all value.
var jArray = ;
var obj = JSON.parse(jArray);
i got [object Object] how i retrive the all value from obj.
my json string is-
{
"Account": "dimts",
"Account_desc": "Adminstrator",
"TimeZone": "Asia/Calcutta",
"DeviceList": [
{
"Device": "dl1pb1831",
"Device_desc": "DL 1PB 1831",
"EventData": [
{
"Device": "dl1pb1831",
"Timestamp": 1387790572,
"Timestamp_date": "2013/12/23",
"Timestamp_time": "14:52:52",
"StatusCode": 61472,
"StatusCode_hex": "0xF020",
"StatusCode_desc": "Location",
"GPSPoint": "28.52802,77.14041",
"GPSPoint_lat": 28.52802,
"GPSPoint_lon": 77.14041,
"Speed": 12.9,
"Speed_units": "km/h",
"Heading": 193.6,
"Heading_desc": "S",
"DigitalInputMask": 3,
"DigitalInputMask_hex": "0x03",
"Index": 0
}
]
},
{
"Device": "dl1pb7520",
"Device_desc": "DL 1PB 7520",
"EventData": [
{
"Device": "dl1pb7520",
"Timestamp": 1387790574,
"Timestamp_date": "2013/12/23",
"Timestamp_time": "14:52:54",
"StatusCode": 61472,
"StatusCode_hex": "0xF020",
"StatusCode_desc": "Location",
"GPSPoint": "28.56589,77.05268",
"GPSPoint_lat": 28.56589,
"GPSPoint_lon": 77.05268,
"Speed": 29.9,
"Speed_units": "km/h",
"Heading": 91.4,
"Heading_desc": "E",
"DigitalInputMask": 3,
"DigitalInputMask_hex": "0x03",
"Index": 0
}
]
},
Look at javascript tutorial
obj['key_name']
JSON objects work as an array. You can access to an element with a key:
obj['Account'] // returns dimts
obj.Account // works also
You should read some tutorial about it, like JSON: What It Is, How It Works, & How to Use It
Please retrive the value as
var jArray = <?php echo json_encode($_SESSION['return'] ); ?>;
var obj = JSON.parse(jArray);
var value=obj.Result;
I have zero experience in php so I don't know what's the resulting object from your first line of code. But assuming jArray is a json object with the structure defined in your question...you access its values as shown below...
jArray.Account;
jArray.DeviceList[0].Device; //access the device property of the first object in the DeviceList array
jArray.DeciveList[0].EventData.StatusCode;

JSON parsing in JS

I am getting a JSON in response from server:
{
"width": "765",
"height": "990",
"srcPath": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_MERGED_/1273.pdf",
"coverPage": "",
"documents": [
{
"index": "1",
"text": "Archiving Microsoft® Office SharePoint® Server 2007 Data with the Hitachi Content Archive Platform and Hitachi Data Discovery for Microsoft SharePoint",
"type": "doc",
"id": "HDS_054227~201106290029",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_2.png"
}
]
},
{
"index": "11",
"text": "Brocade FCoE Enabling Server I/O Consolidation",
"type": "doc",
"id": "HDS_053732~201105261741",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_2.png"
}
]
}
]
}
And I want to get pagelocation of the children.
Can anyone tell me how to do this?
Hi
i also want to get indexes from this and then want to get pagelocations of that particular children. Can you tell me how would i do that?
And also when i when i am getting indexes array it is returning me ,, only and not the index nos.
I am using following code for that :
indexes=response.documents.map(function(e){ return e.children.index; })
Thanks & Regards
If you're interested in simply retrieving all the page locations, you can do it using filter:
var locations = [];
json.documents.forEach(function(e,i) {
e.children.forEach(function(e2,i2) {
locations.push(e2.pageLocation);
)}
});
// returns flat array like [item1,item2,item3,item4]
You can get an array of arrays using map:
var locations = [];
var locations = json.documents.map(function(e) {
return e.children.map(function(e2) {
return e2.pageLocation;
});
});
// returns 2-dimensional array like [[item1,item2],[item1,item2]]
Your json response is an appropriate javascript object So you can access all elements of the object like you do as in back end.
here, you have an array of object of the type documents and each document object has array of objects of the type children. so
syntax would be
myjson.documents[0].children[0].pagelocation
( = http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png)
will give you the very first page location..
and so on

Json returns every character as a separate object?

I have a json object that I'm loading from wordpress using the JSON API plugin. When I load the json object and try to log out the parts of it, it seems like it treats every single character as its own object so the loop returns me a couple thousand objects all with item in it which is a single character. This is my first time using json so idk if i'm missing a step here. this is the code I'm using so far.
function getProjInfo(theId){
$.ajax({// calling the ajax object of jquery
type: "GET",// we are going to be getting info from this data source
url: 'http://testing.charter21.com/api/get_post/?post_id='+ theId,//the datasource
dataType: "application/json",
success: function(data){
parseJson(data);
}, // what happens when it is successful at loading the XML
error: function(){
alert("error");
}
});
}//end of the function
function parseJson(inData){
postInfo = inData;
$.each(postInfo, function(index, value){
console.log(this);
});
}
the json looks like this:
{
"status": "ok",
"count": 10,
"count_total": 19,
"pages": 2,
"posts": [
{
"id": 175,
"type": "post",
"slug": "home-page-slider",
"url": "http:\/\/testing.charter21.com\/uncategorized\/home-page-slider\/",
"status": "publish",
"title": "Home Page slider",
"title_plain": "Home Page slider",
"content": "<p>The cImages in here are the images that are in the slider on the home page this content in this box will not show up. Its only here as a guide.<\/p>\n",
"excerpt": "The cImages in here are the images that are in the slider on the home page this content in this box will not show up. Its only here as a guide.",
"date": "2011-01-25 10:40:25",
"modified": "2011-01-25 10:40:25",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "admin",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "open"
}
so basically instead of giving me "status" as an key and "ok" as a value, i get "s" as an object with an index 0 that has a value of "s" for every single character in the json object. Any help on this matter would be appreciated.
You need to set dataType:json in your $.ajax() request so that jQuery converts the JSON-formatted string into a JavaScript object for you to process as such. You're currently using application/json which is a mime type, and not a valid value for this field in a jQuery request.
In your case you can even try data = eval(data) , this javascript statement should convert your string to json object.
Use the Jquery function:
data = $.parseJSON(data);
before using $.each.
The way I solved it in my AngularJS app is by sending the response from the server (I'm using Node Express) as a JavaScript object, rather than as a string. Nothing else worked for me.
var response = {};
response.error = "Error message";
res.send(response);

Categories

Resources