How to access the below json value - javascript

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}

Related

Updating an Array that's present inside an Object

I'm trying to update an array (Array name is "Variables" please refer the attached screenshot) which presents inside an Object, so I want to update that array if there is word called "placeholder" in alertMessage(it's a different property presents in the same Object)
I'd appreciate any help on how to update this array in question, I tried using pop method but it didn't go as planned and I've attached screenshots of the Objects for reference
You can retrieve the string placeholder like this data['alertMessage']['en_US']['all'] and then use a conditional statement to make changes to the array inside the data object.
let data = {
alertOne: '',
alertTwo: '',
alertMessage: {
en_US: {all: 'placeholder'}
},
variables: [
{id: 0, uuid: '123'},
{id: 1, uuid: '223'},
{id: 2, uuid: '323'}
]
}
let all = data['alertMessage']['en_US']['all']
// if condition is met add a new object to the array
if(all === 'placeholder'){
data.variables = [...data.variables, {id: 3, uuid: '423'}]
}
console.log(data)

Accessing Mongoose Object fields and values

I have a piece of my ticket schema structured as so -
ticketAssignment : [{
isAssigned : {type: Boolean, default:false},
assignee : {type: String, default: null}
}]
when I console.log ticket.ticketAssignment[0] I receive the following -
{ isAssigned: true, assignee: '5ddc6e151c816d4154ab32d7' }
My question is how would I go about accessing just the individual fields i.e.
isAssigned: true
AND
assignee: '5ddc6e151c816d4154ab32d7'
EDIT:
using ticket.ticketAssignment[0].assignee yields an error Cannot read property 'assignee' of undefined which is strange since the property of ticket.ticketAssignment[0] is defined before I try and access the assignee/isAssigned fields of the object.
using tickets[i].ticketAssignment[0]['assignee'] generates the exact same error.

Printing Object doesnt contain all keys

Im completely lost. This is some test code I use to print a specific key of an object, then im printing the entire object.
console.log(docs[0].mc_ign);
console.log(docs[0]);
Now this is the output I see on the console:
The__TxT
{
id: 0,
status: 1,
testing: false,
_id: 5dbc17eb20b3a8594d569570,
timestamp: 2019-11-01T11:32:59.380Z,
mc_uuid: 'dac89e44d1024f3b810478ed62d209a1',
discord_id: '653029505457586176',
email_address: 'gut97930#eveav.com',
country: 'Germany',
birth_month: 3,
birth_year: 1943,
about_me: 'about me text',
motivation: 'motivation text',
build_images: '',
publish_about_me: true,
publish_age: false,
publish_country: true,
__v: 0
}
Where is the mc_ign key?
The object itself comes from mongoose, the missing key is added by me after the fact:
docs[i].mc_ign = mc_ign;
I tried logging the entire object before and after I add the key and assign the value. They are both the same.
What am I missing? Why can I read the value out, but cant see it?
It is mongoose document object. To achieve what you want do the following.
docs[0] = docs[0].toObject();
docs[0].mc_ign = "stuff";
console.log(docs[0])
.toObject() convert it to plain JS object.

Why JS object console log gives NaN instead of Number?

From .NET backend we receive by GET method to Google Chrome an object
Delivery {
Id: 1,
ShippingDateTime: null,
ApproximateHandoverDateTime: null,
Charge: 527,
Cost: null
}
If we look at the preview of GET method Delivery.Charge is 527.
BUT if we type console.log(Delivery) it will give that Delivery.Charge is NaN !!!
Moreover if we type console.log(Delivery.Charge) it will give 527 !
What is going on? Please, explain!
UPDATE
If even after GET method I type Delivery.Charge = 123 anyway console.log(Delivery) gives me Delivery.Charge as NaN. Crazy!
Change your property name to anything but "Charge".
For instance, Charge -> DeliveryCharge:
Delivery {
Id: 1,
ShippingDateTime: null,
ApproximateHandoverDateTime: null,
DeliveryCharge: 527,
Cost: null
}
The correct format of the object should be:
var Delivery = {
key:value,
key:value
};
I think Delivery is an object and you can not print it out directly.
You have to write a method that the same toString() in Java.
See here for example.
var Delivery = {
Id: 1,
ShippingDateTime: null,
ApproximateHandoverDateTime: null,
Charge: 527,
Cost: null
}
function test(){
alert(JSON.stringify(Delivery)) //you use stringify to get all elements of oject.
}

Struggling with json array

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);
});
});

Categories