Trying to get a deeply embedded object from API object in JavaScript - javascript

I am trying to pull out the "quantity" value out of this API call with JavaScript however I cant seem to wrap my mind around it. Does any one feel up for the challenge?
{
"count": 5,
"results": [{
"listing_id": 216653218,
"state": "active",
"user_id": 57517426,
"category_id": 69150359,
"title": "Foodie Dice",
"description": "Some discription.",
"creation_tsz": 1419964059,
"ending_tsz": 1430414859,
"original_creation_tsz": 1419964059,
"last_modified_tsz": 1420033299,
"price": "25.00",
"currency_code": "USD",
"**quantity**": 1,
"tags": [],
"category_path": ["Geekery"],
"category_path_ids": [69150359],
"materials": [],
"shop_section_id": null,
"featured_rank": null,
"state_tsz": 1419964059,
"url": "https://www.etsy.com/listing/216653218/foodie-dice?utm_source=buddhabar&utm_medium=api&utm_campaign=api",
"views": 1,
"num_favorers": 0,
"shipping_template_id": null,
"processing_min": 1,
"processing_max": 1,
"who_made": "i_did",
"is_supply": "false",
"when_made": "2010_2015",
"is_private": false,
"recipient": null,
"occasion": null,
"style": null,
"non_taxable": false,
"is_customizable": false,
"is_digital": false,
"file_data": "",
"language": "en-US",
"has_variations": false,
"used_manufacturer": false,
"MainImage": {
"listing_image_id": 704444582,
"hex_code": null,
"red": null,
"green": null,
"blue": null,
"hue": null,
"saturation": null,
"brightness": null,
"is_black_and_white": null,
"creation_tsz": null,
"listing_id": 216653218,
"rank": null,
"url_75x75": "https://img0.etsystatic.com/048/0/10392050/il_75x75.704444582_poqv.jpg",
"url_170x135": "https://img0.etsystatic.com/048/0/10392050/il_170x135.704444582_poqv.jpg",
"url_570xN": "https://img0.etsystatic.com/048/0/10392050/il_570xN.704444582_poqv.jpg",
"url_fullxfull": "https://img0.etsystatic.com/048/0/10392050/il_fullxfull.704444582_poqv.jpg",
"full_height": null,
"full_width": null
}
},

Use a tool to beautify the output, it helps a lot.
One such tool here http://jsbeautifier.org/
As far as your question, you can access it here:
data.results[0].quantity

Related

Use API JSON to display a URL for an HTML Page

I am completely new to all of this so please forgive any issues with how I'm describing and name things. I have an HTML page where the user enters a portion of a URL string in order to launch a new window with the complete URL string. I'd rather just lookup the portion of the URL using an API to create a JSON file and find it automatically. Any ideas how I can accomplish this without user intervention?
HTML (contains text box and button to launch a new window, which I would like to bypass this altogether)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Cast Challonge</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(e) {
var inputvalue = $("#input").val();
window.open(("http://challonge.com/"+inputvalue+"/module?theme=5928&selected-round-
filter=2&show_final_results=0&multiplier=2&show_tournament_name=1&scale_to_fit=1"),"Tournament
Display","width=1280,height=720,resizable=no, toolbar=no, scrollbars=no, menubar=no, status=no,
directories=no");
});
});
</script>
</head>
<body>
<input type="text" value="" id="input">
<button type="button" id="button">Submit Tournament ID</button>
</body>
</html>
My sample API call is as follows:
[
{
"tournament": {
"id": 9838153,
"name": "2021/05/22 Saturday Night 8-Ball",
"url": "BnkSat0522",
"description": "",
"tournament_type": "double elimination",
"started_at": "2021-05-23T07:01:41.429+07:00",
"completed_at": "2021-05-23T12:54:30.331+07:00",
"require_score_agreement": false,
"notify_users_when_matches_open": true,
"created_at": "2021-05-23T03:41:49.015+07:00",
"updated_at": "2021-05-23T12:54:30.580+07:00",
"state": "complete",
"open_signup": false,
"notify_users_when_the_tournament_ends": true,
"progress_meter": 100,
"quick_advance": false,
"hold_third_place_match": false,
"pts_for_game_win": "0.0",
"pts_for_game_tie": "0.0",
"pts_for_match_win": "1.0",
"pts_for_match_tie": "0.5",
"pts_for_bye": "1.0",
"swiss_rounds": 0,
"private": false,
"ranked_by": "match wins",
"show_rounds": true,
"hide_forum": false,
"sequential_pairings": false,
"accept_attachments": false,
"rr_pts_for_game_win": "0.0",
"rr_pts_for_game_tie": "0.0",
"rr_pts_for_match_win": "1.0",
"rr_pts_for_match_tie": "0.5",
"created_by_api": false,
"credit_capped": false,
"category": null,
"hide_seeds": false,
"prediction_method": 0,
"predictions_opened_at": null,
"anonymous_voting": false,
"max_predictions_per_user": 1,
"signup_cap": null,
"game_id": 773,
"participants_count": 24,
"group_stages_enabled": false,
"allow_participant_match_reporting": false,
"teams": false,
"check_in_duration": null,
"start_at": null,
"started_checking_in_at": null,
"tie_breaks": [
"match wins vs tied",
"game wins",
"points scored"
],
"locked_at": null,
"event_id": null,
"public_predictions_before_start_time": false,
"ranked": false,
"grand_finals_modifier": null,
"predict_the_losers_bracket": false,
"spam": null,
"ham": null,
"rr_iterations": 1,
"tournament_registration_id": null,
"donation_contest_enabled": null,
"mandatory_donation": null,
"non_elimination_tournament_data": {
"participants_per_match": ""
},
"auto_assign_stations": null,
"only_start_matches_with_stations": null,
"registration_fee": "0.0",
"registration_type": "free",
"split_participants": false,
"allowed_regions": [
],
"show_participant_country": null,
"program_id": null,
"program_classification_ids_allowed": null,
"team_size_range": null,
"toxic": null,
"description_source": "",
"subdomain": null,
"full_challonge_url": "https://challonge.com/BnkSat0522",
"live_image_url": "https://challonge.com/BnkSat0522.svg",
"sign_up_url": null,
"review_before_finalizing": true,
"accepting_predictions": false,
"participants_locked": true,
"game_name": "8-ball",
"participants_swappable": false,
"team_convertable": false,
"group_stages_were_started": false
}
},
{
"tournament": {
"id": 9845539,
"name": "2021/05/24 Monday Night 10-Ball",
"url": "20210524",
"description": "",
"tournament_type": "double elimination",
"started_at": "2021-05-25T06:34:46.053+07:00",
"completed_at": "2021-05-25T10:54:03.406+07:00",
"require_score_agreement": false,
"notify_users_when_matches_open": true,
"created_at": "2021-05-25T04:01:56.922+07:00",
"updated_at": "2021-05-25T10:54:03.589+07:00",
"state": "complete",
"open_signup": false,
"notify_users_when_the_tournament_ends": true,
"progress_meter": 100,
"quick_advance": false,
"hold_third_place_match": false,
"pts_for_game_win": "0.0",
"pts_for_game_tie": "0.0",
"pts_for_match_win": "1.0",
"pts_for_match_tie": "0.5",
"pts_for_bye": "1.0",
"swiss_rounds": 0,
"private": false,
"ranked_by": "match wins",
"show_rounds": true,
"hide_forum": false,
"sequential_pairings": false,
"accept_attachments": false,
"rr_pts_for_game_win": "0.0",
"rr_pts_for_game_tie": "0.0",
"rr_pts_for_match_win": "1.0",
"rr_pts_for_match_tie": "0.5",
"created_by_api": false,
"credit_capped": false,
"category": null,
"hide_seeds": false,
"prediction_method": 0,
"predictions_opened_at": null,
"anonymous_voting": false,
"max_predictions_per_user": 1,
"signup_cap": null,
"game_id": 6341,
"participants_count": 22,
"group_stages_enabled": false,
"allow_participant_match_reporting": true,
"teams": false,
"check_in_duration": null,
"start_at": null,
"started_checking_in_at": null,
"tie_breaks": [
"match wins vs tied",
"game wins",
"points scored"
],
"locked_at": null,
"event_id": null,
"public_predictions_before_start_time": false,
"ranked": false,
"grand_finals_modifier": null,
"predict_the_losers_bracket": false,
"spam": null,
"ham": null,
"rr_iterations": 1,
"tournament_registration_id": null,
"donation_contest_enabled": null,
"mandatory_donation": null,
"non_elimination_tournament_data": {
"participants_per_match": ""
},
"auto_assign_stations": false,
"only_start_matches_with_stations": false,
"registration_fee": "0.0",
"registration_type": "free",
"split_participants": false,
"allowed_regions": [
],
"show_participant_country": null,
"program_id": null,
"program_classification_ids_allowed": null,
"team_size_range": null,
"toxic": null,
"description_source": "",
"subdomain": null,
"full_challonge_url": "https://challonge.com/20210524",
"live_image_url": "https://challonge.com/20210524.svg",
"sign_up_url": null,
"review_before_finalizing": true,
"accepting_predictions": false,
"participants_locked": true,
"game_name": "10-Ball",
"participants_swappable": false,
"team_convertable": false,
"group_stages_were_started": false
}
},
{
"tournament": {
"id": 9851001,
"name": "2021/05/26 Wednesday Night 9-Ball",
"url": "20210526",
"description": "",
"tournament_type": "double elimination",
"started_at": "2021-05-26T20:48:53.085+07:00",
"completed_at": null,
"require_score_agreement": false,
"notify_users_when_matches_open": true,
"created_at": "2021-05-26T20:46:47.342+07:00",
"updated_at": "2021-05-26T20:48:53.158+07:00",
"state": "underway",
"open_signup": false,
"notify_users_when_the_tournament_ends": true,
"progress_meter": 0,
"quick_advance": false,
"hold_third_place_match": false,
"pts_for_game_win": "0.0",
"pts_for_game_tie": "0.0",
"pts_for_match_win": "1.0",
"pts_for_match_tie": "0.5",
"pts_for_bye": "1.0",
"swiss_rounds": 0,
"private": false,
"ranked_by": "match wins",
"show_rounds": true,
"hide_forum": true,
"sequential_pairings": false,
"accept_attachments": false,
"rr_pts_for_game_win": "0.0",
"rr_pts_for_game_tie": "0.0",
"rr_pts_for_match_win": "1.0",
"rr_pts_for_match_tie": "0.5",
"created_by_api": false,
"credit_capped": false,
"category": null,
"hide_seeds": false,
"prediction_method": 0,
"predictions_opened_at": null,
"anonymous_voting": false,
"max_predictions_per_user": 1,
"signup_cap": null,
"game_id": 485,
"participants_count": 2,
"group_stages_enabled": false,
"allow_participant_match_reporting": false,
"teams": false,
"check_in_duration": null,
"start_at": null,
"started_checking_in_at": null,
"tie_breaks": [
"match wins vs tied",
"game wins",
"points scored"
],
"locked_at": null,
"event_id": null,
"public_predictions_before_start_time": false,
"ranked": false,
"grand_finals_modifier": null,
"predict_the_losers_bracket": false,
"spam": null,
"ham": null,
"rr_iterations": 1,
"tournament_registration_id": null,
"donation_contest_enabled": null,
"mandatory_donation": null,
"non_elimination_tournament_data": {
"participants_per_match": ""
},
"auto_assign_stations": null,
"only_start_matches_with_stations": null,
"registration_fee": "0.0",
"registration_type": "free",
"split_participants": false,
"allowed_regions": [
],
"show_participant_country": null,
"program_id": null,
"program_classification_ids_allowed": null,
"team_size_range": null,
"toxic": null,
"description_source": "",
"subdomain": null,
"full_challonge_url": "https://challonge.com/20210526",
"live_image_url": "https://challonge.com/20210526.svg",
"sign_up_url": null,
"review_before_finalizing": true,
"accepting_predictions": false,
"participants_locked": true,
"game_name": "9-ball",
"participants_swappable": false,
"team_convertable": false,
"group_stages_were_started": false
}
}
]
Where I am trying to grab a tournament that the state is "underway" and return "url" that can then be used to launch a complete URL as described above.
I've created the following:
<?php
$apikey = 'MyAPIKEY';
$contents_api = fopen("https://api.challonge.com/v1/tournaments.json?
state=all&api_key=$apikey", "r");
$json = stream_get_contents($contents_api);
$jsonData = json_decode($json, TRUE);
print $jsonData[count($jsonData)-1]['tournament']['url'];
?>
Now I need to figure out how to grab this information and load the page!!!!
Again, I know this is probably not the best way to ask however, any guidance pointing me in the correct direction would be very helpful.
I was able to get what I wanted using:
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
$apikey = 'MYAPIKEY';
$contents_api = fopen("https://api.challonge.com/v1/tournaments.json?state=all&api_key=$apikey", "r");
$json = stream_get_contents($contents_api);
$jsonData = json_decode($json, TRUE);
//print $jsonData[count($jsonData)-1]['tournament']['url'];
$inputvalue = $jsonData[count($jsonData)-1]['tournament']['url'];
$url= "http://challonge.com/$inputvalue/module?selected-round-filter=2&show_final_results=0&multiplier=2&show_tournament_name=1&scale_to_fit=1"
?>
<html>
<head>
<script>
location.replace("<?php echo $url; ?>");
</script>
<meta http-equiv="refresh" content="<?php echo $sec ?>;URL='<?php echo $page?>'">
</head>
</html>

Not able to read below JSON using Angular 7

I was not able to read below mentioned received JSON from Rest API.
{
"details": {
"id": 21,
"gstin": "27AACCG7831Q1Z0",
"entityName": "GENX ENTERTAINMENT LIMITED",
"entityPan": "AACCG7831Q",
"entityTan": "abhishek123",
"entityCin": "null",
"entityWebsite": null,
"natureOfBusiness": null,
"constitution": "\"Public Limited Company\""
},
"addresses": [
{
"id": 22,
"buildingName": "\"Solitaire Corporate Park\"",
"location": null,
"street": null,
"buildingNo": "\"S-14\"",
"state": "bihar",
"district": "\"\"",
"city": "\"\"",
"flateNo": "\"1\"",
"pincode": "\"400093\"",
"isPrimary": false
}
]
}
I want to use above data received in JSON format into a form.
Your question is not clear, but you can pass this values to a form using this :
const formValues = JSON.parse(yourJson);
this.form.values = formValues;
See The Stakblitz Code Below
stackblit_sampleCode
Hope this helps..!

How to get the dataValueField when I select one node

I am using kendoTreeView. I want to get the unitId when one node is selected.
This is my code.
$("#treeview").kendoTreeView({
dataSource: dataSource,
dataTextField: "name",
dataValueField: 'unitId'
});
dataSource example:
{
"rows": [{
"_token": "8cfd3e2133d936a6a65c6f7cfb80268a",
"objectVersionNumber": null,
"unitId": 10002,
"parentId": 10001,
"unitCode": "100000",
"name": "Hand",
"description": null,
"managerPosition": null,
"companyId": null,
"enabledFlag": null,
"unitCategory": null,
"unitType": null,
"positionName": null,
"parentName": null,
"parentCode": null,
"hasChildren": true
}],
"success": true,
"total": 1
}
Which method of treeview are you using to capture the click event? You can write a select event that kendoTreeView provides which will give you data of the node you clicked/selected. You can refer this: https://docs.telerik.com/kendo-ui/api/javascript/ui/treeview/events/select
If this doesn't work for you, can you share your DOJO or jsFiddle and I could further help.

Cant read React state data

I'm developing a react application and in my state I have the data saved that I try to read. When I JSON.stringify the device data "console.log"'it out, look like this:
{
"ActTime": 1509988664,
"ServerTime": "2017-11-06 18:17:44",
"Sunrise": "07:25",
"Sunset": "15:53",
"result": [
{
"AddjMulti": 1,
"AddjMulti2": 1,
"AddjValue": 0,
"AddjValue2": 0,
"BatteryLevel": 255,
"CustomImage": 0,
"Data": "On",
"Description": "",
"Favorite": 1,
"HardwareID": 2,
"HardwareName": "Controller",
"HardwareType": "OpenZWave USB",
"HardwareTypeVal": 21,
"HaveDimmer": true,
"HaveGroupCmd": true,
"HaveTimeout": false,
"ID": "00000501",
"Image": "Light",
"IsSubDevice": false,
"LastUpdate": "2017-11-06 15:42:00",
"Level": 0,
"LevelInt": 0,
"MaxDimLevel": 100,
"Name": "Vardagsrum",
"Notifications": "false",
"PlanID": "0",
"PlanIDs": [
0
],
"Protected": false,
"ShowNotifications": true,
"SignalLevel": "-",
"Status": "On",
"StrParam1": "",
"StrParam2": "",
"SubType": "Switch",
"SwitchType": "On/Off",
"SwitchTypeVal": 0,
"Timers": "false",
"Type": "Light/Switch",
"TypeImg": "lightbulb",
"Unit": 1,
"Used": 1,
"UsedByCamera": false,
"XOffset": "0",
"YOffset": "0",
"idx": "3"
}
],
"status": "OK",
"title": "Devices"
}
If I want to read the status data I just do: device['status'] I get "OK", but what if I want to access the result data in the device?
I thought that device['result'][0]['Status'] would give my 'Ok, but I just get Uncaught TypeError: Cannot read property '0' of undefined ???
How do I read the result data in the device??
To do this device['result'][0]['Status'] you want to ensure your device data is not a string but a proper JSON object. You could JSON.parse() to convert it if it's not already an object.
Also ensure the data is available before trying that.
Try to use map method over result array.
Something like this:
result.map((item,index)=>{
console.log(item[index].AddjMulti);
})

soundcloud api - how to check recorded track is active or not?

How can I check recorded track is uploaded and active for embed.
I want to show embed preview of track once it upload is done.
but I am getting 404 error from scPlayer api while resolving the url.
Is possible to track availability of track, based on that i can use sCplayer()
for resolving track.
Any help?
Thanks
Peter
I know in the /tracks resource there is a attribute called state which could be what you're looking for.
On their docs, the example value they give is finished, so maybe it would read uploading or processing before the track is ready. NB: I haven't tried this myself.
{
"id": 13158665,
"created_at": "2011/04/06 15:37:43 +0000",
"user_id": 3699101,
"duration": 18109,
"commentable": true,
"state": "finished",
"sharing": "public",
"tag_list": "soundcloud:source=iphone-record",
"permalink": "munching-at-tiannas-house",
"description": null,
"streamable": true,
"downloadable": true,
"genre": null,
"release": null,
"purchase_url": null,
"label_id": null,
"label_name": null,
"isrc": null,
"video_url": null,
"track_type": "recording",
"key_signature": null,
"bpm": null,
"title": "Munching at Tiannas house",
"release_year": null,
"release_month": null,
"release_day": null,
"original_format": "m4a",
"original_content_size": 10211857,
"license": "all-rights-reserved",
"uri": "http://api.soundcloud.com/tracks/13158665",
"permalink_url": "http://soundcloud.com/user2835985/munching-at-tiannas-house",
"artwork_url": null,
"waveform_url": "http://w1.sndcdn.com/fxguEjG4ax6B_m.png",
"user": {
"id": 3699101,
"permalink": "user2835985",
"username": "user2835985",
"uri": "http://api.soundcloud.com/users/3699101",
"permalink_url": "http://soundcloud.com/user2835985",
"avatar_url": "http://a1.sndcdn.com/images/default_avatar_large.png?142a848"
},
"stream_url": "http://api.soundcloud.com/tracks/13158665/stream",
"download_url": "http://api.soundcloud.com/tracks/13158665/download",
"playback_count": 0,
"download_count": 0,
"favoritings_count": 0,
"comment_count": 0,
"created_with": {
"id": 124,
"name": "SoundCloud iPhone",
"uri": "http://api.soundcloud.com/apps/124",
"permalink_url": "http://soundcloud.com/apps/iphone"
},
"attachments_uri": "http://api.soundcloud.com/tracks/13158665/attachments"
}
from: http://developers.soundcloud.com/docs/api/tracks
alternatively, there could be another value, like duration or stream_url that will read NULL until the track is ready.

Categories