Json parse javascript : String displayed as NAN [duplicate] - javascript

This question already has answers here:
Unable to access JSON property with "-" dash [duplicate]
(5 answers)
Closed 5 years ago.
So i am trying parse a json file. I am using alert to get the value of a particular field but whenever i pass this particular string it is displayed as nan
myjsondata=JSON.parse(json);
alert(myjsondata.result.parameters.College-name);
Json file
`{
"id": "1",
"timestamp": "2017-05-11T04:03:26.008Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "hi",
"action": "input.welcome",
"actionIncomplete": false,
"parameters": {
"College-name": "Apex Technical School"},
"contexts": [],
"metadata": {
"intentId": "b11a9493-7c2f-47c0-9928-5653a10c86e9",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Default Welcome Intent"
},
"fulfillment": {
"speech": "Hi welcome from webfocus Api Ai",
"messages": [
{
"type": 0,
"speech": "Hi welcome from webfocus Api Ai"
},
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "04737548-a3ff-485d-af1a-304edfee9486"
}`
alert with action and other fields are working fine . But for college it is displayed as NAN

You are getting a null value because parameters does not have a key called College-name:
"parameters": {},
Also, you might want to change your selection be using:
alert(myjsondata.result.parameters['College-name']);

Related

Protobuf decoding uint64 invalid in Chrome Extension

I'm trying to get decrypted information from the Steam using protobuf, but since it contains a uint64, the javascript immediately writes it as a number into the decrypted json and, due to its length, this number turns into a rounded number. I do not understand how to decrypt the protobuff message so that instead of a crooked number, get the correct string with uint64 data.
Response proto:
"CAuthentication_BeginAuthSessionViaCredentials_Response": {
"fields": {
"client_id": {
"type": "uint64",
"id": 1,
"jstype": "JS_STRING",
"options": {"jstype": "JS_STRING", "(description)": "unique identifier of requestor, also used for routing"}
},
"request_id": {
"type": "bytes",
"id": 2,
"options": {"(description)": "unique request ID to be presented by requestor at poll time - must not be transferred or displayed"}
},
"interval": {
"type": "float",
"id": 3,
"options": {"(description)": "refresh interval with which requestor should call PollAuthSessionStatus"}
},
"allowed_confirmations": {
"rule": "repeated",
"type": ".CAuthentication_AllowedConfirmation",
"id": 4,
"options": {"(description)": "the confirmation types that will be able to confirm the request"}
},
"steamid": {
"type": "uint64",
"id": 5,
"options": {"jstype": "JS_STRING", "(description)": "steamid of the account logging in - will only be included if the credentials were correct"}
},
"weak_token": {
"type": "string",
"id": 6,
"options": {"(description)": "partial-authentication token - limited lifetime and scope, included only if credentials were valid"}
}
}
}
Site response in decode:
{
...
steamid: 76561198140356100
...
}
Original data is 76561198140356099 , but JS Convert it to 76561198140356100.
Binary for example:
new Uint8Array([10,128,4,97,97,98,49,56,52,100,49,55,50,48,102,53,98,99,52,98,49,52,51,102,48,57,56,102,99,51,49,56,48,51,101,101,100,102,50,101,52,100,99,48,100,55,50,56,102,97,101,53,53,51,53,55,55,50,56,53,99,51,57,102,52,48,100,55,101,51,57,52,57,101,101,98,97,50,52,100,54,97,53,54,57,53,49,52,52,98,49,102,98,100,100,52,56,55,49,51,56,49,54,54,100,101,98,53,54,102,50,102,53,48,49,101,54,52,53,97,57,52,50,97,54,102,49,49,101,53,50,100,97,50,57,98,52,98,102,54,99,54,48,98,102,102,48,52,50,50,52,49,97,97,53,55,100,97,99,99,48,98,100,102,56,97,98,49,56,55,51,49,52,48,48,48,102,53,50,48,54,102,99,57,102,50,48,52,97,55,97,50,100,98,56,52,57,97,98,101,57,57,51,48,101,57,56,55,48,49,54,97,100,52,102,56,57,101,52,49,54,50,48,98,55,102,56,102,57,99,97,48,52,99,49,98,100,98,49,52,57,56,99,49,57,101,53,98,54,100,55,48,50,55,100,51,98,54,102,50,52,48,55,53,50,56,97,102,56,98,99,100,49,51,102,50,48,98,51,48,97,56,55,102,99,100,55,50,51,101,53,101,101,55,52,99,48,55,54,102,101,100,101,99,57,99,56,99,55,52,54,56,50,55,56,53,98,54,55,55,101,100,52,100,50,100,99,56,56,56,54,98,57,98,54,51,56,99,56,97,54,48,54,97,48,51,49,99,98,100,53,100,48,57,98,99,101,52,53,99,53,48,99,48,57,100,99,55,98,50,53,50,97,51,97,100,57,100,53,52,99,99,55,52,52,49,49,100,50,55,51,57,52,98,52,97,54,101,53,54,54,53,53,51,57,49,102,57,54,54,52,52,48,99,51,56,100,99,102,48,97,55,52,50,97,49,102,57,55,101,101,98,100,99,53,56,50,55,101,50,54,57,97,102,55,100,52,48,99,99,102,99,54,56,55,99,57,99,52,100,50,56,97,56,55,99,54,49,54,55,100,51,55,55,97,48,98,100,55,56,57,54,49,48,98,99,98,49,102,57,57,99,48,54,97,52,49,55,56,57,57,99,55,100,50,49,53,102,100,98,101,55,53,55,49,18,6,48,49,48,48,48,49,24,240,200,254,191,132,16]).buffer;
Proto for exampled buffer:
"CAuthentication_GetPasswordRSAPublicKey_Response": {
"fields": {
"publickey_mod": {
"type": "string",
"id": 1,
"options": {"(description)": "the public key modulus"}
},
"publickey_exp": {"type": "string", "id": 2, "options":{"(description)": "the public key exponent"}},
"timestamp": {
"type": "uint64",
"id": 3,
"options": {"jstype": 1, "(description)": "the timestamp the key was generated"}
}
}
}
To solve the problem, you need to get a json object from the buffer data in which the time stamp value will not be a number, but a string.

Access a JSON attribute's value in a big nested array without using attribute name [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
How to iterate over a JavaScript object?
(19 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 years ago.
This is the JSON array:
{
"head": {
"status": "200",
"data": {}
},
"body": {
"content": {
"deliveryMessage": {
"MT9J2AE/A": {
"label": "Delivers:",
"quote": "Sep 23-Sep 24",
"address": {
"city": "Ajman"
},
"showDeliveryOptionsLink": false,
"messageType": "Delivery",
"basePartNumber": "MT9J2",
"commitCodeId": "8886",
"idl": false,
"defaultLocationEnabled": false
},
"MT9M2AE/A": {
"label": "Delivers:",
"quote": "25 Sep - 2 Oct",
"address": {
"city": "Ajman"
},
"showDeliveryOptionsLink": false,
"messageType": "Delivery",
"basePartNumber": "MT9M2",
"commitCodeId": "199",
"idl": false,
"defaultLocationEnabled": false
},
"geoLocated": false,
"deliveryLocationLink": {
"text": "Ajman",
"dataVar": {},
"newTab": false
},
"dudeCookieSet": true,
"MT9F2AE/A": {
"label": "Delivers:",
"quote": "25 Sep - 2 Oct",
"address": {
"city": "Ajman"
},
"showDeliveryOptionsLink": false,
"messageType": "Delivery",
"basePartNumber": "MT9F2",
"commitCodeId": "199",
"idl": false,
"defaultLocationEnabled": false
},
"deliveryLocationLabel": "Your delivery location:",
"WarmAPUWithDude": true,
"locationCookieValueFoundForThisCountry": true,
"dudeLocated": true,
"accessibilityDeliveryOptions": "delivery options",
"little": true
}
}
}
}
I want to access the three "quote" attribute's values.
So far this is what I have achieved:
parsedata = JSON.parse(jsondata);
parsedata[0].body.content.deliveryMessage["MT9J2AE/A"].quote
This will give the "quote" value of "MT9J2AE/A". But if that attribute changes, how do I get the quotes?
I mean I am asking something like this:
parsedata[0].body.content.deliveryMessage[0].quote
But that is not working :(

trouble while parsing json

I am trying to react displayName in req
when I write
console.log(req.session.passport.user._raw)
the output is:
{
"kind": "plus#person",
"etag": "\"ucaTEV-ZanNH5M3SCxYRM0QRw2Y/XiR7kPThRbzcIw-YLiARoF22TMY\"",
"emails": [
{
"value": "rajanlagah#gmail.com",
"type": "account"
}
],
"objectType": "person",
"id": "100428644453494579140",
"displayName": "Rajan Lagah",
"name": {
"familyName": "Lagah",
"givenName": "Rajan"
},
"url": "https://plus.google.com/100428644453494579140",
"image": {
"url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
"isDefault": true
},
"isPlusUser": true,
"language": "en",
"circledByCount": 0,
"verified": false
}
Now the from this object I am trying to get kind (say)
console.log(req.session.passport.user._raw.kind)
then the output is undefined
Can any one tell my mistake?
Is req.session.passport.user._raw a string? You may need to parse it first, eg:
var data = JSON.parse(req.session.passport.user._raw);
console.log(data.kind);

How to get response from an API that returns same values with different answers? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
(Sorry if the title doesn't make much sense, I couldn't figure out how to word it correctly)
So I'm trying to get the response from an API, but the API returns multiple responses with the same name.
Ex of a response:
{
"xuid": 2535436668322645,
"state": "Online",
"devices": [
{
"type": "XboxOne",
"titles": [
{
"id": 714681658,
"name": "Home",
"placement": "Background",
"state": "Active",
"lastModified": "2016-11-22T23:45:08.8296994Z"
},
{
"id": 74304278,
"activity": {
"richPresence": "Lvl 30 in Hudson Yards"
},
"name": "Tom Clancy's The Division",
"placement": "Full",
"state": "Active",
"lastModified": "2016-11-22T23:45:08.8296994Z"
}
]
}
]
}
So I was wondering, how would I be able to get the ID from the "second" response using JavaScript? (The one that says "Tom Clancy's The Division" in the name part) Thanks!
This should get you on the right track (untested)
var obj = jQuery.parseJSON(YOUR_JSON);
obj->devices[0]->titles[1]->id // second id

Access to elements of JSON array of objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
My data on server is in the form of JSON array of objects. If I want to print the first object, how can I access its first object? Format of data is:
[
{
"eventId": "8577",
"datasetId": "34",
"nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 35, Hum : 83",
"datasetName": "test active set",
"mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
"gpsLong": "-46.633301",
"contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:16",
"updatedOn": "2015-04-21 08:03:16"
},
{
"eventId": "8576",
"datasetId": "34",
"nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 34, Hum : 81",
"datasetName": "test active set",
"mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
"gpsLong": "-46.633301",
"contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:11",
"updatedOn": "2015-04-21 08:03:11"
},
{
"eventId": "8575",
"datasetId": "34",
"nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 33, Hum : 80",
"datasetName": "test active set",
"mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
"gpsLong": "-46.633301",
"contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:05",
"updatedOn": "2015-04-21 08:03:05"
},
]
I tried with data[0], but end up with printing "[". I tried with JSON.stringify also and then split with "," but that gives me first element of the object, not the whole first object. I need to print the whole first object which is within the curly braces. Is there any way so that I can access like array and by doing document.getElementById("Id")=myData[0], so that I don't need to print all elements within the curly braces separately. Please suggest me some solution. Thanks in advance.
use var dataArray = JSON.parse(dataString) first,
your data is a string at the moment.
Then use dataArray[0]

Categories

Resources