I'm working with the last.fm api to get artist images, and I'm getting JSON results where I need to check an '#attr' value. Unfortunately, I can't seem to access this value. The results look something like:
{"image":[{
"url":"http:\/\/www.last.fm\/music\/Undefined\/+images\/3040021",
"format":"jpg",
"sizes":{"size":{"#text":"http:...jpg","name":"original","width":"397","height":"397"},{"#text":"http:...jpg","name":"large","width":"126","height":"126"},]},
"#attr":{"official":"yes"}}
it's that last value that I can't get to...
any ideas?
I tried ['#attr'] and it didnt' seem to work - only returned undefined.
I'm doing an $.each(obj.image, function(){}) - and within i'm successfully getting this.url, this.format, etc - but i'm not having luck with this['#attr']
Use the bracket notation member operator:
var value = obj[0]['#attr'];
Then you can access the official property by:
value.official;
Or
obj[0]['#attr']['official'];
Or
obj[0]['#attr'].official;
Edit: As Jonathan pointed out, you have issues with the JSON structure you post, I would recommend you to validate your JSON with a tool like JSONLint.
But I think that you mean something like this:
var obj = {
"image": [{
"url": "http:\/\/www.last.fm\/music\/Undefined\/+images\/3040021",
"format": "jpg",
"sizes": [{
"#text": "http:...jpg",
"name": "original",
"width": "397",
"height": "397"
},
{
"#text": "http:...jpg",
"name": "large",
"width": "126",
"height": "126"
}
],
"#attr": {
"official": "yes"
}
}]
};
And with that JSON structure you can iterate it by:
$.each(obj.image, function () {
alert(this['#attr'].official);
});
You have quite a few formatting issues in your snippet. If these are the same in your actual JSON, you're going to have parsing and object-structure conflicts from what you're probably expecting.
{ /* no matching end */
"images": [ /* no matching end */
{
"url":"http:\/\/www.last.fm\/music\/Undefined\/+images\/3040021",
"format":"jpg",
"sizes": { /* should this be an array instead? */
"size": {
"#text":"http:...jpg",
"name":"original",
"width":"397",
"height":"397"
},
{ /* missing key */
"#text":"http:...jpg",
"name":"large",
"width":"126",
"height":"126"
}, /* trailing comma can cause parsing issues */
] /* no matching start */
},
"#attr": { "official":"yes" }
}
Related
this is my js code
let content019 = JSON.parse(require("fs").readFileSync("./settings.json"));
// edit or add property
content019.api.client.locations[1] = 999999999999;
//write file
fs.writeFileSync('settings.json', JSON.stringify(content019,null ,2));
i tried using content019.api.client.locations[1] but it changed the inner part to "1": "999....
this is a part of my json file
"1": {
"name": "Germany",
"type": null
},
"2": {
"name": "Singapore",
"type": null
}
},
i want it to only change "1": { to "999999999999": {
even tried content019.api.client.locations.1, didnt work. received error unexpected number
you have to use a string, not a number index. Since your json is invalid, I can only assume that the first part is ok, so you can try
content019.api.client.locations["1"] = 999999999999;
or maybe
content019.api.client.locations["999999"] = content019.api.client.locations["1"];
delete content019.api.client.locations["1"];
I'm currently using the fixture file to make sure it will be easier to call the right value.
cy.fixture('latestLead.json').then(function (lead) {
this.lead = lead
})
My son file is the following:
{
"status": 0,
"result": {
"totalSize": 1,
"done": true,
"records": [
{
"attributes": {
"type": "Lead",
"url": "/services/data/v51.0/sobjects/Lead/111111111"
},
"Id": "111111111",
"Name": "Andres Latest Test"
}
]
}
}
The way that I'm trying to get the right value is the following:
cy.get(".gLFyf").type(this.lead.result.records.Id)
I'm able to get totalSize or done from the result object, but I'm not able to get any other value higher than that object. How can I get the Id value from the records Array?
You can access an array item (in your case it's the object) using the index position (in your case it's zero)
cy.get(".gLFyf").type(this.lead.result.records[0].Id)
try this
cy.get(".gLFyf").type(this.lead.result.records[0].Id)
Let's imagine that we have sap.m.UploadCollection and we bind the data to this collection which is done like this:
bind: function () {
this._oUploadCollection.bindAggregation("items", {
path: "/attachments",
factory: jQuery.proxy(this._bindUploadCollectionItem, this)
});
},
The example of the binding data is here:
{
"attachments": [
{
"size": 123,
"filename": "pdf.pdf",
"id": "pdfId"
},
{
"size": 440,
"filename": "text.txt",
"id": "textId"
}
],
"source":"personWhoAddedAttachments"
}
So, in _bindUploadCollectionItem I successfully can get size, filename and id by oContext.getProperty("nameOfParameter"), but cannot get source:
_bindUploadCollectionItem: function (sID, oContext) {
return new sap.m.UploadCollectionItem({
"id": oContext.getProperty("id"),
"fileName": oContext.getProperty("filename"),
"attributes": [
{
"title": "author",
"text": oContext.getProperty("../source") // <- problem
}]
});
},
So, because I bind attachments it is kind of clear that I could not get source, but how to reach it if I need it?
It depends a little on what property of the model you want to get to. If it is really like you described it and the target property is in the /source absolute model path, then the easiest way of getting it inside the factory function is by using: oContext.getModel().getProperty("/source").
If you need something which is inside a collection (and somehow depends on the current context), you can achieve an effect similar to the .. path construct that you tried by using something along the lines:
var sPath = oContext.getPath(),
sParent = sPath.substring(0, sPath.lastIndexOf("/")),
sText = oContext.getModel().getProperty(sParent + "/source");
return new sap.m.UploadCollectionItem({
"id": oContext.getProperty("id"),
"fileName": oContext.getProperty("filename"),
"attributes": [{
"title": "author",
"text": sText
}]
});
You basically obtain the parent object path by searching for the last / inside the path. You can apply this repeatedly (or use a split, pop some elements, followed by a join) to get to the ancestors (e.g. parent of parent).
I want to get the req1 value in the JSON below, programatically.Here RequestTypeItem can be changed as well, so it is not fixed. Else I could have navigated it using object.subobject
I was able to navigate till slots using
var b = JSON.parse("{ .... }");
b.request.intent.slots.RequestTypeItem.value
But I can navigate further programatically.
{"request": {
"locale": "en-US",
"timestamp": "2016-09-25T00:36:14Z",
"type": {
"name": "request",
"slots": {
"RequestTypeItem": {
"name": "RequestTypeItem",
"value": "req1"
}
}
}
}
}
In your JSON your request does not have a property of intent, it does have a property type, so you then you can access the property you want with
b.request.type.slots.RequestTypeItem.value
JSFiddle: https://jsfiddle.net/9cexbn54/
Edit: After reading your question again, maybe this is what you want:
// loop through all properties on the slots object
for (var i in b.request.type.slots) {
if (b.request.type.slots.hasOwnProperty(i)) { // make sure it is a property belonging directly to slots, and not "inherited" from the prototype chain
if (b.request.type.slots[i].value) { // make sure that the sub-property of slots has a value property
document.getElementById("output").innerHTML = b.request.type.slots[i].value;
break; // break out of the loop after getting a value
}
}
}
Here I loop through all the properties on slots, checking that the property does indeed belong to slots, and that it has value property.
JSFiddle: https://jsfiddle.net/9cexbn54/1/
I had given a link which some of you found it not useful.
Here is what would get to req1:
$data=#'
[{"request": {
"locale": "en-US",
"timestamp": "2016-09-25T00:36:14Z",
"type": {
"name": "request",
"slots": {
"RequestTypeItem": {
"name": "RequestTypeItem",
"value": "req1"
}
}
}
}
}]
'#
$json=ConvertFrom-Json $data
$json.request.type.slots.RequestTypeItem.value
I have an JSON array like this
var filter_value_data = [{"Status":[{"name":"Open","id":"1"},{"name":"Pending","id":"2"},{"name":"Resolved","id":"3"},{"name":"Closed","id":"4"},{"name":"Evaluation","id":"5"}]},{"Payment Status":[{"name":"Paid","id":"10"},{"name":"UnPaid","id":"11"},{"name":"Part Paid","id":"12"}]},{"Priority":[{"name":"Low","id":"6"},{"name":"Medium","id":"7"},{"name":"High","id":"8"},{"name":"Urgent","id":"9"}]}]
I have tried filter_value_data["Status"] which is obviously wrong. How do I get the JSON elements for Status using the names like Status,Payment Status?
filter_value_data is an array (having []), so use filter_value_data[0].Status to get the first element-object with property "Status".
It is always good to format your code in order to see the hierarchy of the structures:
var filter_value_data = [
{
"Status": [
{
"name": "Open",
"id": "1"
}, {
"name": "Pending",
"id": "2"
}, ...
]
}, {
"Payment Status": [
{
"name": "Paid",
"id": "10"
}, ...
]
}, {
"Priority": [
{
"name": "Low",
"id": "6"
}, ...
]
}
];
With your current JSON you can't get the elements with the name alone.
You can get Status with filter_value_data[0]['Status'] and Payment status with filter_value_data[1]['Payment Status'].
This is because the keys are in seperate objects in the array.
In order to get them with filter_value_data['Status'] you need to change your JSON to
var filter_value_data = {
"Status":[
{"name":"Open","id":"1"},
{"name":"Pending","id":"2"},
{"name":"Resolved","id":"3"},
{"name":"Closed","id":"4"},
{"name":"Evaluation","id":"5"}
],
"Payment Status":[
{"name":"Paid","id":"10"},
{"name":"UnPaid","id":"11"},
{"name":"Part Paid","id":"12"}
],
"Priority":[
{"name":"Low","id":"6"},
{"name":"Medium","id":"7"},
{"name":"High","id":"8"},
{"name":"Urgent","id":"9"}
]
};
I wrote this on my phone so it's not as well-formatted as usual. I'll change it ASAP.
With your current JSON, created a result which might be helpful for you.
JS:
$.each(filter_value_data,function(ind,val){
var sta = val.Status; // Status Object get displayed
for(var i=0;i<sta.length;i++){
var idVal= sta[i].id;
var nameVal = sta[i].name;
Statusarray.push(idVal,nameVal);
console.log(Statusarray);
}
})
FiddleDemo
You can use below code, it will return status object
filter_value_data[0]['Status']
filter_value_data[0]['Payment Status']
to get Single value you use :
filter_value_data[0]['Status'][0]['name']