get data from multidimensional json object [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to get the result of the following json object returned by google driving api.
"routes": [
{
"bounds": {
"Ga": {
"C": 39.01115,
"j": 39.06483
},
"xa": {
"j": -77.57857000000001,
"C": -77.47601
}
},
"copyrights": "Map data ©2015 Google",
"legs": [
{
"distance": {
"text": "9.4 mi",
"value": 15096
},
"duration": {
"text": "15 mins",
"value": 870
},
"end_address":"1234 Any St USA".....//and so on
how would i go about getting the data in "text" or "end_address"
i have tried
alert(result.routes.legs.distance.text)
and
alert(result.routes.legs.end_address);
and i get
Uncaught TypeError: Cannot read property 'distance' of undefined
and
Uncaught TypeError: Cannot read property 'end_address' of undefined
respectively

routes is an array and legs is an array within each of the routes array objects
Try
alert(routes[0].legs[0].distance.text)

Related

when i write ગુજરાતી but show ગુજરાતી in html why? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
when i write ગુજરાતી but show ગુજરાતી in html why ??
let jsq_questions = [
{
"id": 1,
"question": "ગુજરાતી ",
"options": {
"a": "શ્રી વિરાટ કોહલી",
"b": "શ્રી બાબર આઝમ",
"c": "શ્રી પેટ કમિન્સ",
"d": "શ્રી બેન સ્ટોક્સ"
},
"answer": "b"
}
but show
how to get normal language

Check is JSON is valid [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to get JSON in a valid format so that it can be sent to an API and I can't figure out why the below JSON is not valid. Please can someone explain why this is not valid?
{
"Description": "test",
"Quantity": "0.30",
"UnitAmount": "6400.0",
"TaxType": "OUTPUT2",
"AccountCode": "200"
},
{
"Description": "test2",
"Quantity": "0.30",
"UnitAmount": "0.0",
"TaxType": "OUTPUT2",
"AccountCode": "200"
}
The top level of a JSON text must be one of the JSON data types (like object, array, or string).
There can only be one data type at the top level.
You have an object but then you have a comma and then a second object.
Perhaps you should wrap it in an array.

how to nested data from inside a JSON [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
const data = [{
"detectedLanguage": {
"language": "hi",
"score": 1
},
"translations": [{
"text": "long steff",
"to": "en"
}]
}]
How do I console.log only the translation.text field?
I tried console.log(JSON.stringify(res.data.translation.text) but that gives me the following error:
TypeError: Cannot read property 'text' of undefined
Try this,
console.log(data[0].translations[0].text)
No need to use Json.stringify, because data[0].translations[0].text is not a Json, it is a string.
You created arrays and am missing the index and the "s" in translations:
console.log(JSON.stringify(data[0].translations[0].text))
I also don't know why you're referencing it as res.data

How to parse string with break lines to object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Is there a possible way to parse a string with breaklines to an object using JSON.parse?
const text = '{ "name": "Anne", "desc": "Hi,\nThis is me" }';
const obj = JSON.parse(text);
console.log(obj);
There are two things here:
To answer your question, you simply need to escape the character like this:
"Hi,\\nThis is me"
For your code specifically, you also have another syntax error with a ,, instead of a ::
const text = '{ "name": "Anne", "desc": "Hi,\\nThis is me" }';
const obj = JSON.parse(text);
console.log(obj);
output:
{
name: "Anne" ,
desc: "Hi, This is me"
}

Javascript how to properly access associative array elements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am having a bit of difficulty understanding how to access elements from a 2 dimension associative array. For example, when I try using the "price" key from the first array, it returns as object Object. I am having a hard time returning the values that the keys are related to. I have tried without quotations also. Thanks for your time.
var houseData = Array(
{
"price": "$320, 000",
"imgsrc": "./images/colonial.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$459, 999",
"imgsrc": "./images/contemporary.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$179, 500",
"imgsrc": "./images/cottage.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$210, 000",
"imgsrc": "./images/ranch.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$159, 999",
"imgsrc": "./images/townhouse.jpg",
"description": "Colonial House, 3 bedrooms"
});
Basically, brackets in JS will create an object. With the Array function, you are creating a list of object. So as #Salehen Rahman said, you need to first access the object in the list by specifying an index :
var objectAtIndex0 = houseData[0];
and then access the data in the object:
var priceFromObjectAtIndex0 = objectAtIndex0.price;
or one-line:
var priceFromObjectAtIndex0 = houseData[0].price;

Categories