Indexing last number of a JSON Object - javascript

So I have a JSON object, here it is:
{
"868": {
"header": "New limited",
"lite": "1337 Gaming Headset",
"icon": "",
"items": { "Stock": "1,337", "Price": "R$750" },
"extra": { "product": 25355494 },
"url": "",
"id": 868,
"added": 1438542256903
},
"869": {
"header": "New Face",
"lite": "Furious George",
"icon": "",
"items": { "Price": "R$200" },
"extra": { "product": 25355932 },
"url": "",
"id": 869,
"added": 1438543456863
},
"870": {
"header": "Almost sold out",
"lite": "1337 Gaming Headset",
"icon": "",
"items": { "Stock": "1,337", "Remaining": "133", "Price": "R$750" },
"extra": { "product": 25355494 },
"url": "",
"id": 870,
"added": 1438544588831
},
"871": {
"header": "Sold out",
"lite": "1337 Gaming Headset",
"icon": "",
"items": { "Price": "R$750" },
"extra": { },
"url": "",
"id": 871,
"added": 1438544704049
},
"872": {
"header": "New Hat",
"lite": "Elite Spy Eye",
"icon": "",
"items": { "Price": "R$250" },
"extra": { "product": 25356879 },
"url": "",
"id": 872,
"added": 1438545677167
}
}
I want to know how to index the last thing of this json object. I can't describe it any more but it might be the key.
I want to basically index the last object in the object, so that would be the bit where it says "872":{"header":"New Hat" near the bottom
So basically it's the biggest number I want to index. I already tried to index the last one with [4] as an index but it came up with undefined.

I suppose that you might want this:
var str = '{"868":{"header":"New limited","lite":"1337 Gaming Headset","icon":"","items":{"Stock":"1,337","Price":"R$750"},"extra":{"product":25355494},"url":"","id":868,"added":1438542256903},"869":{"header":"New Face","lite":"Furious George","icon":"","items":{"Price":"R$200"},"extra":{"product":25355932},"url":"","id":869,"added":1438543456863},"870":{"header":"Almost sold out","lite":"1337 Gaming Headset","icon":"","items":{"Stock":"1,337","Remaining":"133","Price":"R$750"},"extra":{"product":25355494},"url":"","id":870,"added":1438544588831},"871":{"header":"Sold out","lite":"1337 Gaming Headset","icon":"","items":{"Price":"R$750"},"extra":{},"url":"","id":871,"added":1438544704049},"872":{"header":"New Hat","lite":"Elite Spy Eye","icon":"","items":{"Price":"R$250"},"extra":{"product":25356879},"url":"","id":872,"added":1438545677167}}';
var obj = JSON.parse(str);
var last = Object.keys(obj).sort(function(a, b) {
return parseInt(b) - parseInt(a);
})[0]; // suppose to be "872" in this case
console.log('"' + last + '": ' + JSON.stringify(obj[last]));

var json = {
"868": {
"header": "New limited",
"lite": "1337 Gaming Headset",
"icon": "",
"items": { "Stock": "1,337", "Price": "R$750" },
"extra": { "product": 25355494 },
"url": "",
"id": 868,
"added": 1438542256903
},
"869": {
"header": "New Face",
"lite": "Furious George",
"icon": "",
"items": { "Price": "R$200" },
"extra": { "product": 25355932 },
"url": "",
"id": 869,
"added": 1438543456863
},
"870": {
"header": "Almost sold out",
"lite": "1337 Gaming Headset",
"icon": "",
"items": { "Stock": "1,337", "Remaining": "133", "Price": "R$750" },
"extra": { "product": 25355494 },
"url": "",
"id": 870,
"added": 1438544588831
},
"871": {
"header": "Sold out",
"lite": "1337 Gaming Headset",
"icon": "",
"items": { "Price": "R$750" },
"extra": {},
"url": "",
"id": 871,
"added": 1438544704049
},
"872": {
"header": "New Hat",
"lite": "Elite Spy Eye",
"icon": "",
"items": { "Price": "R$250" },
"extra": { "product": 25356879 },
"url": "",
"id": 872,
"added": 1438545677167
}
}
var keys = Object.keys(json);
var last = keys[keys.length - 1];
document.write('"' + last + '": ' + JSON.stringify(json[last]));

Related

Merging two array of objects only when array element contains error

So I have two arrays with objects. And also an array indicating where the replacement should take place(It spots if object contains error)
const oldData = [
{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Sasko",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "ERROR: Invalid UoM type, valid values are: kg,g,mg,kl,l,ml,m,cm,mm",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "ERROR: Empty sku_id is not allowed",
"name": "Sasko Black1",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Future Life",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-099",
"name": "Sasko Black100",
"barcode": "843332555614",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "702",
"capacity": "3",
"image": ""
},
{
"index": "04",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "ERROR: Invalid description: [] it should not be blank.",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
},
{
"index": "05",
"skuId": "Sarbb-101",
"name": "Sasko Black102",
"barcode": "843332555616",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "704",
"capacity": "5",
"image": ""
}
]
const newData = [
{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "sarb",
"name": "Sasko Black1",
"barcode": "124125125",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "214214214",
"description": "Desc",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
}
]
const errorRows = [0,1,3]
const myTerribleAttempt = oldTableData.map((oldData, rowIndex) => {
return errorRows.map(index => {
if(rowIndex === index){
newTableData.map(newData => {
oldData = newData
})
}
})
})
I have tried multiple maps and just can't seem to get the right result. The new data objects should replace old data objects at the object containing the error. Please give me some assistance.
I think you've overcomplicated this - just check if the index is contained in the error array and choose old or new data.
const result = oldData.map( (item, idx) => errorRows.includes(idx)
? newData[errorRows.findIndex(x => x == idx)]
: item);
live example:
const oldData = [{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Sasko",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "ERROR: Invalid UoM type, valid values are: kg,g,mg,kl,l,ml,m,cm,mm",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "ERROR: Empty sku_id is not allowed",
"name": "Sasko Black1",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Future Life",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-099",
"name": "Sasko Black100",
"barcode": "843332555614",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "702",
"capacity": "3",
"image": ""
},
{
"index": "04",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "ERROR: Invalid description: [] it should not be blank.",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
},
{
"index": "05",
"skuId": "Sarbb-101",
"name": "Sasko Black102",
"barcode": "843332555616",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "704",
"capacity": "5",
"image": ""
}
]
const newData = [{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "sarb",
"name": "Sasko Black1",
"barcode": "124125125",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "214214214",
"description": "Desc",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
}
]
const errorRows = [0, 1, 3];
const result = oldData.map((item, idx) => errorRows.includes(idx) ? newData[idx] : item);
console.log(result);

I want to change a json format after fetching using javascript

I have a json like below..after fetching this json i stored in one variable called data.I need to change this data variable as bellow
{
"recipes": [
{
"cuisine": "chinese",
"description": "hjkhdkd",
"id": 3,
"min_time": "30-60min",
"name": "Noodles",
"nutrition": "",
"qty": "",
"quantity_unit_id": "2",
"rate": "",
"recipe_type": "vegetarian",
"serve": [
"4"
],
"tip": "",
"image_url": {
"medium": "/system/recipes/images/000/000/003/medium/dfdfs.jpeg?1501851554"
}
}
]
}
how i can change above json data like bellow using javascript
[
{
"cuisine": "chinese",
"description": "hjkhdkd",
"id": 3,
"min_time": "30-60min",
"name": "Noodles",
"nutrition": "",
"qty": "",
"quantity_unit_id": "2",
"rate": "",
"recipe_type": "vegetarian",
"serve": [
"4"
],
"tip": "",
"image_url": {
"medium": "/system/recipes/images/000/000/003/medium/dfdfs.jpeg?1501851554"
}
}
]
var data = '{ "recipes": [ { "cuisine": "chinese", "description": "hjkhdkd", "id": 3, "min_time": "30-60min", "name": "Noodles", "nutrition": "", "qty": "", "quantity_unit_id": "2", "rate": "", "recipe_type": "vegetarian", "serve": [ "4" ], "tip": "", "image_url": { "medium": "/system/recipes/images/000/000/003/medium/dfdfs.jpeg?1501851554" } } ] }';
var jsonifieddata = JSON.parse(data);
var newdata = jsonifieddata.recipes;
JSON.stringify(newdata);
Run this in your console.
A suggestion: share some effort you have made when you ask on stackoverflow again.

JavaScript time comparison with arrays

I'm working on a project where I need to get all the vacant classrooms for the day and basically filter out the ones being in use.
I get all the data from the school's own API with JSON response body that looks like this:
{
"status": "success",
"reservations": [
{
"id": "18935",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:51",
"startDate": "2017-04-27T11:00:00",
"endDate": "2017-04-27T13:00:00",
"resources": [
{
"id": "50",
"type": "room",
"code": "A440.5",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A440.5"
},
{
"id": "2995",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2267",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20362",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T11:00:00",
"endDate": "2017-04-27T14:00:00",
"resources": [
{
"id": "51",
"type": "room",
"code": "A450.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A450.1"
},
{
"id": "2402",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3064",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20237",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T11:15:00",
"endDate": "2017-04-27T13:00:00",
"resources": [
{
"id": "45",
"type": "room",
"code": "A420.4",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A420.4"
},
{
"id": "2433",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3058",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20888",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:57",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "62",
"type": "room",
"code": "A520.5",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A520.5"
},
{
"id": "3092",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2444",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "22586",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:48",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "52",
"type": "room",
"code": "A450.3",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A450.3"
},
{
"id": "3004",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2294",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "525",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18816",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:58",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "41",
"type": "room",
"code": "A340.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A340.1"
},
{
"id": "2989",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "795",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "599",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20431",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:56",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "40",
"type": "room",
"code": "A320.7",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A320.7/8"
},
{
"id": "2416",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2386",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18588",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:49",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "25",
"type": "room",
"code": "A130.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A130.1"
},
{
"id": "26",
"type": "room",
"code": "A130.3",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A130.3"
},
{
"id": "2979",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "582",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18940",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:53",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "2996",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2267",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "2268",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "31",
"type": "room",
"code": "A210.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A210.2"
}
],
"description": ""
},
{
"id": "12041",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:53",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "2510",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "775",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "23",
"type": "room",
"code": "A520.7",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A520.7"
}
],
"description": ""
},
{
"id": "24630",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "3277",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "42",
"type": "room",
"code": "A340.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A340.2"
}
],
"description": ""
},
{
"id": "27205",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:07",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "35",
"type": "room",
"code": "A240.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A240.2"
},
{
"id": "775",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3384",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "25917",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:00",
"startDate": "2017-04-27T15:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "36",
"type": "room",
"code": "A240.4",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A240.4"
},
{
"id": "593",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "595",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "21932",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:06",
"startDate": "2017-04-27T16:00:00",
"endDate": "2017-04-27T18:00:00",
"resources": [
{
"id": "43",
"type": "room",
"code": "A350.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A350.1"
},
{
"id": "2464",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "2747",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
}
]
}
In order to get the JSON response I need to put startDate (the moment user searches the vacant rooms, in this situation it is: 2017-04-27T10:55) and endDate (set to end of the day, 2017-04-27T22:00). The result for this query is the JSON response above.
The problem is that the API I'm using doesn't contain any data for the vacant rooms but only for the ones booked for certain times so I made a list of all the rooms in the building and compared it to the booked ones to filter them out:
var rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3','A450.1',
'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7','A540.1', 'A540.2'];
var data = JSON.parse(responseText);
var booking = Object.create(null);
var free;
data.reservations.forEach(function (reservation) {
reservation.resources.some(function (resource) {
if (resource.type === 'room') {
booking[resource.code] = booking[resource.code] || [];
booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
return true;
}
});
});
free = rooms.filter(function (a) {
return !booking[a];
});
console.log(free);
Results:
A210.3
A220.5
A320.2
A520.6
A510.4
This only returns the ones that are not being used at all during the day but I need to get the ones that are vacant for hour or two.
For this booked class for example:
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"type": "room",
"code": "A520.5"
for this I would need to print out:
A520.5 - 2 hours 20 minutes
So I need to get the room / time from the startDate of the search (2017-04-27T10:55) and compare it to the startDate of the booked room (2017-04-27T13:15:00) to get the time remaining for that room to be vacant.
TO CLARIFY:
I have all the data for booked rooms starting from 2017-04-27T10:55
until 2017-04-27T22:00 as you can see from the JSON response above.
I need to somehow compare the startDate of the JSON query when the user
searches for the rooms (2017-04-27T10:55) and compare it to the booked
rooms startDate to get the time how long the room stays vacant.
This is how I'm doing my startDate and endDate for the JSON query:
// Timestamp needs to be formed (YYYY-MM-DDTxx:xx) in order for JSON query to work
var todaysDate = new Date();
function convertDate(date) {
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth() + 1).toString();
var dd = date.getDate().toString();
var mmChars = mm.split('');
var ddChars = dd.split('');
return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
}
// Current time when user searches for vacant rooms
var currentTime = new Date();
var time = "T" + currentTime.getHours() + ":" + currentTime.getMinutes();
// variables for the JSON query
var startDate = convertDate(todaysDate) + time;
var endDate = convertDate(todaysDate) + ("T22:00");
// JSON-query
var getRooms = {
"startDate": startDate,
"endDate": endDate
};

Detect track playing or current time of mixcloud embed?

I have a mixcloud show embedded on my page.
For example:
<iframe width="660" height="180" src="//www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2Fdjzhao%2Fjuju-juke-shangaan-footwork%2F&embed_uuid=cd8d4d1b-9940-4548-9aff-1f411f5480c0&replace=0&hide_cover=1&embed_type=widget_standard&hide_tracklist=1" frameborder="0"></iframe>
I'd like to be able to change the text displayed depending on which track is currently being played. Is there a way to detect which track is being played or the current play time and change the text accordingly?
The mixcloud api can return the information for a cloudcast that includes the track list and time at which they start like so:
{
"description": "Given I'm on the Mixcloud team, I thought I really should have something up here. Here's an hour's worth of funky music to get you ready for partying, enjoy the music and ignore the quality (or lack thereof) of the DJ!",
"tags": [
{
"url": "http://www.mixcloud.com/tag/funky-house/",
"name": "Funky house",
"key": "/tag/funky-house/"
},
{
"url": "http://www.mixcloud.com/tag/funk/",
"name": "Funk",
"key": "/tag/funk/"
},
{
"url": "http://www.mixcloud.com/tag/soul/",
"name": "Soul",
"key": "/tag/soul/"
}
],
"play_count": 1864,
"user": {
"url": "http://www.mixcloud.com/spartacus/",
"username": "spartacus",
"name": "Spartacus",
"key": "/spartacus/",
"pictures": {
"medium": "http://images-mix.netdna-ssl.com/w/100/h/100/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"320wx320h": "http://images-mix.netdna-ssl.com/w/320/h/320/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"extra_large": "http://images-mix.netdna-ssl.com/w/600/h/600/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"large": "http://images-mix.netdna-ssl.com/w/300/h/300/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"640wx640h": "http://images-mix.netdna-ssl.com/w/640/h/640/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"medium_mobile": "http://images-mix.netdna-ssl.com/w/80/h/80/q/75/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"small": "http://images-mix.netdna-ssl.com/w/25/h/25/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg",
"thumbnail": "http://images-mix.netdna-ssl.com/w/50/h/50/q/85/upload/images/profile/dfefd1a8-3932-47e1-bbd3-ac5fd2144d80.jpg"
}
},
"key": "/spartacus/party-time/",
"created_time": "2009-08-02T16:55:01Z",
"audio_length": 3361,
"slug": "party-time",
"favorite_count": 11,
"listener_count": 90,
"name": "Party Time",
"url": "http://www.mixcloud.com/spartacus/party-time/",
"pictures": {
"medium": "http://images-mix.netdna-ssl.com/w/100/h/100/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"320wx320h": "http://images-mix.netdna-ssl.com/w/320/h/320/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"extra_large": "http://images-mix.netdna-ssl.com/w/600/h/600/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"large": "http://images-mix.netdna-ssl.com/w/300/h/300/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"640wx640h": "http://images-mix.netdna-ssl.com/w/640/h/640/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"medium_mobile": "http://images-mix.netdna-ssl.com/w/80/h/80/q/75/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"small": "http://images-mix.netdna-ssl.com/w/25/h/25/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png",
"thumbnail": "http://images-mix.netdna-ssl.com/w/50/h/50/q/85/upload/images/extaudio/61a1279f-e3c0-4871-aa8e-c4cf5466edb8.png"
},
"updated_time": "2013-10-15T13:48:53Z",
"comment_count": 3,
"sections": [
{
"track": {
"url": "http://www.mixcloud.com/track/jazztronik/samurai-12-mix/",
"artist": {
"url": "http://www.mixcloud.com/artist/jazztronik/",
"name": "Jazztronik",
"key": "/artist/jazztronik/",
"slug": "jazztronik"
},
"name": "Samurai (12\" Mix)",
"key": "/track/jazztronik/samurai-12-mix/",
"slug": "samurai-12-mix"
},
"position": 1,
"start_time": 0,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/time-of-your-life/refresher/",
"artist": {
"url": "http://www.mixcloud.com/artist/time-of-your-life/",
"name": "Time of your life",
"key": "/artist/time-of-your-life/",
"slug": "time-of-your-life"
},
"name": "Refresher",
"key": "/track/time-of-your-life/refresher/",
"slug": "refresher"
},
"position": 2,
"start_time": 416,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/dutch/my-time-feat-crystal-waters/",
"artist": {
"url": "http://www.mixcloud.com/artist/dutch/",
"name": "Dutch",
"key": "/artist/dutch/",
"slug": "dutch"
},
"name": "My time (feat. Crystal Waters)",
"key": "/track/dutch/my-time-feat-crystal-waters/",
"slug": "my-time-feat-crystal-waters"
},
"position": 3,
"start_time": 716,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/minimal-funk/definition-of-house/",
"artist": {
"url": "http://www.mixcloud.com/artist/minimal-funk/",
"name": "Minimal Funk",
"key": "/artist/minimal-funk/",
"slug": "minimal-funk"
},
"name": "Definition of House",
"key": "/track/minimal-funk/definition-of-house/",
"slug": "definition-of-house"
},
"position": 4,
"start_time": 1061,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/mint-royale/i-dont-know/",
"artist": {
"url": "http://www.mixcloud.com/artist/mint-royale/",
"name": "Mint Royale",
"key": "/artist/mint-royale/",
"slug": "mint-royale"
},
"name": "I dont know",
"key": "/track/mint-royale/i-dont-know/",
"slug": "i-dont-know"
},
"position": 5,
"start_time": 1500,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/michael-jackson/thrill-her/",
"artist": {
"url": "http://www.mixcloud.com/artist/michael-jackson/",
"name": "Michael Jackson",
"key": "/artist/michael-jackson/",
"slug": "michael-jackson"
},
"name": "Thrill Her",
"key": "/track/michael-jackson/thrill-her/",
"slug": "thrill-her"
},
"position": 6,
"start_time": 1763,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/elio-isola/happy-featcharlise/",
"artist": {
"url": "http://www.mixcloud.com/artist/elio-isola/",
"name": "Elio Isola",
"key": "/artist/elio-isola/",
"slug": "elio-isola"
},
"name": "Happy (feat.Charlise)",
"key": "/track/elio-isola/happy-featcharlise/",
"slug": "happy-featcharlise"
},
"position": 7,
"start_time": 2123,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/erick-morillo-harry-romero-jose-nunez-feat-jessica-eve/dancin/",
"artist": {
"url": "http://www.mixcloud.com/artist/erick-morillo-harry-romero-jose-nunez-feat-jessica-eve/",
"name": "Erick Morillo, Harry Romero & Jose Nunez feat. Jessica Eve",
"key": "/artist/erick-morillo-harry-romero-jose-nunez-feat-jessica-eve/",
"slug": "erick-morillo-harry-romero-jose-nunez-feat-jessica-eve"
},
"name": "Dancin",
"key": "/track/erick-morillo-harry-romero-jose-nunez-feat-jessica-eve/dancin/",
"slug": "dancin"
},
"position": 8,
"start_time": 2442,
"section_type": "track"
},
{
"track": {
"url": "http://www.mixcloud.com/track/kosheen/all-in-my-head/",
"artist": {
"url": "http://www.mixcloud.com/artist/kosheen/",
"name": "Kosheen",
"key": "/artist/kosheen/",
"slug": "kosheen"
},
"name": "All in my head",
"key": "/track/kosheen/all-in-my-head/",
"slug": "all-in-my-head"
},
"position": 9,
"start_time": 2738,
"section_type": "track"
}
]
}
Thanks for any help
MixCloud released in 2014 a JavaScript API/SDK which communicates with the iframe:
https://www.mixcloud.com/developers/widget/
It uses window.postMessage for all communication with the iframe.
For example:
<iframe id="mixcloud-iframe" width="100%" height="60" src="https://www.mixcloud.com/widget/iframe/?feed=https://www.mixcloud.com/thisgreedypig/the-pigs-ear-money-ex-records-show-case/&hide_cover=1&mini=1&light=1" frameborder="0"></iframe>
<script src="//widget.mixcloud.com/media/js/widgetApi.js" type="text/javascript"></script>
<script type="text/javascript">
var widget = Mixcloud.PlayerWidget(document.getElementById('mixcloud-iframe'));
function togglePlayPause () {
// handle the ui
}
widget.ready.then(function() {
widget.events.pause.on(togglePlayPause);
widget.events.play.on(togglePlayPause);
});
</script>

Extracting data from JSON using javascript

I have JSON similar to this . I wish to extract values like name, his id, and product title from this list . But I am not able to figure it out . I was trying "eval" for the same.
{
"data": [{
"id": "3092773937557",
"from": {
"id": "1810306393",
"name": "Prashant Singh"
},
"start_time": "2012-07-21T09:12:53+0000",
"end_time": "2012-07-21T09:12:53+0000",
"publish_time": "2012-07-21T09:12:53+0000",
"application": {
"id": "132692593533721",
"name": "Compare Hatke"
},
"data": {
"productname": "Apple iPod Nano",
"price": 399,
"product": {
"id": "10151004296768984",
"url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
"type": "comparehatke:product",
"title": "Apple iPod Nano"
}
},
"likes": {
"count": 0
},
"comments": {
"count": 0
},
"no_feed_story": false
}, {
"id": "3092770217464",
"from": {
"id": "1810306393",
"name": "Prashant Singh"
},
"start_time": "2012-07-21T09:08:53+0000",
"end_time": "2012-07-21T09:08:53+0000",
"publish_time": "2012-07-21T09:08:53+0000",
"application": {
"id": "132692593533721",
"name": "Compare Hatke"
},
"data": {
"productname": "Apple iPod Nano",
"price": 399,
"product": {
"id": "10151004296768984",
"url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
"type": "comparehatke:product",
"title": "Apple iPod Nano"
}
},
"likes": {
"count": 0
},
"comments": {
"count": 0
},
"no_feed_story": false
}],
"paging": {
"next": "https:\/\/graph.facebook.com\/me\/comparehatke:compare\/?access_token=AAAB4rubm4xkBAHRhdjVgx7JxIIvUxImIm31AMxgnqEAOQsAsgZAJjBYUfvzKc8XgxDBg3AzKN1S6QU2dnmtgj7TPcoCiih1RzrL3pLpuZAgGt8eKpq&limit=2&method=get&pretty=0&offset=2"
}
}
<html>
<script style="text/javscript">
var myObject = { "data": [{
"id": "3092741696751",
"from": {
"id": "1810306393",
"name": "Prashant Singh"
},
"start_time": "2012-07-21T08:40:38+0000",
"end_time": "2012-07-21T08:40:38+0000",
"publish_time": "2012-07-21T08:40:38+0000",
"application": {
"id": "132692593533721",
"name": "Compare Hatke"
},
"data1": {
"productname": "Apple iPod Nano",
"price": 399,
"product": {
"id": "10151004296768984",
"url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
"type": "comparehatke:product",
"title": "Apple iPod Nano"
}
},
"likes": {
"count": 0
},
"comments": {
"count": 0
},
"no_feed_story": false
} ]};
alert(myObject.data[0].id);
</script>
</html>
data = JSON.parse(yourJSONString);
If this fails, you likely have an error in your JSON. You can use http://jsonlint.com/ to find and resolve the problem. In the paste above, you're missing your closing ]}.

Categories

Resources