Ive changed a json object recieved from an api and changed it to an array using $.makeArray and now im struggling to get values from this array. Im wanting temp_c and value within weather desc. A print out of the array is below.
[
Object
data: Object
current_condition: Array[1]
0: Object
cloudcover: "50"
humidity: "72"
observation_time: "10:25 AM"
precipMM: "0.1"
pressure: "1005"
temp_C: "13"
temp_F: "55"
visibility: "10"
weatherCode: "122"
weatherDesc: Array[1]
0: Object
value: "Overcast"
__proto__: Object
length: 1
__proto__: Array[0]
weatherIconUrl: Array[1]
winddir16Point: "SSW"
winddirDegree: "210"
windspeedKmph: "19"
windspeedMiles: "12"
__proto__: Object
length: 1
__proto__: Array[0]
request: Array[1]
__proto__: Object
__proto__: Object
You could try:
alert(yourObject.temp_C);
And
alert(yourObject.weatherDesc.value);
You wouldn't need to convert it to an array :)
You don't need to transform the object into an array, just access the properties you need:
var o = object_retrieved_from_api;
o.temp_c;
o.weatherDesc[0].value;
If you convert it into an array, just have to index first object in array:
var a = $.makeArray(object_retrieved_from_api);
a[0].temp_c;
a[0].weatherDesc[0].value;
The whole point of JSON is that it is already a Javascript object, so you don't need any complex parsing logic in order to get the data out. Try out the following code and you'll see how easy it is to get data from a JSON web service.
$.getJSON('your JSON URL here', function(data) {
$.each(data.current_condition, function() {
alert(this.temp_C + "C " + this.weatherDesc[0].value);
});
});
Related
i'm making a call to an api which returns data to the front end in this format
{ name: 'Fred',
data: [{'name': '"10\\" x 45\\" Nice Shirts (2-pack)"', 'price': '$30.25'}]
}
the data property is return as a string and i'm trying to use JSON.parse(response.data) to get it as as array so i can use *ngFor to do iteration but i'm always getting an error
Unexpected token ' in JSON at position 2
SyntaxError: Unexpected token ' in JSON at position 2 (at zone.js:1262:1)
at JSON.parse
Your backend is not returning a valid json. It should be:
{"name": "Fred", "data": [{"name": "\\"10\\\\\\" x 45\\\\\\" Nice Shirts (2-pack)\\"", "price": "$30.25"}]}
Hi I'm trying to access the value DepartmentId but getting error as value doesn't exists.
I can get the values inside the curly braces when I try to access the DepartmentId, I'm getting error.
Below is my map function
r.PrimarySearchResults.map((value) => {
console.log(value)}
)
I can get value.Rank but not value.DepartmentId
Below is the JSON object I got. This is when I expand the Object.
{Rank: "16.9111518859863", DocId:
"17598046715456", Title: "HubSite", SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2",
WebTemplate: "SITEPAGEPUBLISHING", …}
Culture: "en-US"
DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}"
DocId: "17598046715456"
PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7"
Rank: "16.9111518859863"
RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js"
ResultTypeId: "0"
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2"
SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada"
Title: "HubSite"
UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}"
UrlZone: "0"
WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a"
WebTemplate: "SITEPAGEPUBLISHING"
__proto__: Object
Find the below image
Tried arranging your JSON object, seems that DepartmentId is not included in the JSON
{
Rank: "16.9111518859863",
DocId: "17598046715456",
Title: "HubSite",
SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2",
WebTemplate: "SITEPAGEPUBLISHING", }
Culture: "en-US" DepartmentId: "{3d408bfe-9172-4df5-b36e-863c066e9ada}" DocId: "17598046715456" PartitionId: "51ddbb65-42e8-4906-82e4-8d97c6626ef7" Rank: "16.9111518859863" RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js" ResultTypeId: "0" SPSiteUrl: "https://amoghtelkar.sharepoint.com/sites/hubsite2" SiteId: "3d408bfe-9172-4df5-b36e-863c066e9ada" Title: "HubSite" UniqueId: "{F08C7BCE-C886-4A09-AA22-D66879DD5252}" UrlZone: "0" WebId: "edec632a-5671-49bd-a7fe-27a6e851f09a" WebTemplate: "SITEPAGEPUBLISHING"
Try this:
var value = JSON.parse(value);
console.log(value.DepartmentId);
As mentioned above, the full JSON object being returned should be able to be parsed into an object and then gotten.
If your response object is PrimarySearchResults, you should be able to do:
r.PrimarySearchResults.DepartmentId;
// should return {3d408bfe-9172-4df5-b36e-863c066e9ada}
I am creating this JSON object
{
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"image_aspect_ratio": "square"
"elements": [
new CreateBallon(results[rand])
]
}
}
}
I want to set the image_aspect_ratio in only if elements[0].someProp is present.
How can I do that?
Let's clear what JSON is. It is a textual notation for data. If you do anything with the object that involves code of Javascript don't call it JSON so, The object you're talking about is not a JSON but a JavaScript object created via object literal notation.
Now to answer your query, you can't refer to this object before you've fully initialized that object. So what you can do is initialize your object with some default value and then you should be able to edit any property based on the properties from the same object. Something like
var obj = {"property1" : "X", "property2" : ""};
Now you can update your object like
obj.property2 = obj.property1 =='yourCondition' ? 'NewValue' : obj.property2
Basically I have to list all video which are in 1:Array(10). and I'm unable to get data from this so please help me
Object {1: Array(10), message: "Get all category list", status: "200", result: Array(1)}
If your property is named 1, than you will not be able to do Object.1 . However, you can do this Object["1"]
You have an object as a parameter so just:
const array = obj['1'];
array.forEach( item => {
//do something here
});
I cant access JSON data from javascript. Please help me how to access data from JSON data in javascript.
i have a JSON data like
{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}
i have tried console.log(data) but log print object object
success:function(data){
console.log(data);
}
How to print console.log particular data?
I need to print
quantity-row_122 = 1
price-row_122 = 35.1
console.log(JSON.stringify(data)) will do what you need. I'm assuming that you're using jQuery based on your code.
If you're wanting those two particular values, you can just access those and pass them to log.
console.log(data.input_data['quantity-row_122']);
console.log(data.input_data['price-row_122']);
I used '%j' option in console.log to print JSON objects
console.log("%j", jsonObj);
To output an object to the console, you have to stringify the object first:
success:function(data){
console.log(JSON.stringify(data));
}
{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}
console.dir() will do what you need. It will give you a hierarchical structure of the data.
success:function(data){
console.dir(data);
}
like so
> Object
> input_data: Object
price-row_122: " 35.1 "
quantity-row_122: "1"
success: true
I don't think you need console.log(JSON.stringify(data)).
To get the data you can do this without stringify:
console.log(data.success); // true
console.log(data.input_data['quantity-row_122']) // "1"
console.log(data.input_data['price-row_122']) // " 35.1 "
Note
The value from input_data Object will be typeof "1": String, but you can convert to number(Int or Float) using ParseInt or ParseFloat, like so:
typeof parseFloat(data.input_data['price-row_122'], 10) // "number"
parseFloat(data.input_data['price-row_122'], 10) // 35.1
I usually do like this:
console.log(JSON.stringify(data, undefined, 4));
If you just want to print object then
console.log(JSON.stringify(data)); //this will convert json to string;
If you want to access value of field in object then use
console.log(data.input_data);
You can also use util library:
const util = require("util")
> myObject = {1:2, 3:{5:{6:{7:8}}}}
{ '1': 2, '3': { '5': { '6': [Object] } } }
> util.inspect(myObject, {showHidden: true, depth: null})
"{\n '1': 2,\n '3': { '5': { '6': { '7': 8 } } }\n}"
> JSON.stringify(myObject)
'{"1":2,"3":{"5":{"6":{"7":8}}}}'
original source : https://stackoverflow.com/a/10729284/8556340
This is an old post but I'm chiming in because (as Narem briefly mentioned) a few of the printf-like features are available with the console.log formatters. In the case of the question, you can benefit from the string, number or json formatters for your data.
Examples:
console.log("Quantity %s, Price: %d", data.quantity-row_122, data.price-row_122);
console.log("Quantity and Price Data %j", data);
Object
input_data: Object
price-row_122: " 35.1 "
quantity-row_122: "1"
success: true