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
Related
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.
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"
}
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 have other JSON data similar to this, hence, I would like to filter it.
const data = [{
"1": {items: { Part1: true, Part2: true } },
id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
displayName: name1
}]
However, I keep getting the error of
Cannot read properties of undefined (reading 'items')
const filter = data.filter(
(a) =>
a.["1"].items.Part1 == true
);
How do I fix this?
you have an extra "." right before ["1"]
a["1"].items.Part1
By the way, if i were you, i would rename items by "item", because it's not an array, just an object.
You are accessing elemet incorrectly. please use this
const filter = data.filter(
(a) =>
a["1"].items.Part1 == true
);
error was here, the extra dot(.) after a => a.["1"], it should be a["1"]
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 5 years ago.
Improve this question
I have a nested array. It is about user information. I need to take the age from the user and add it to my array. I have tried some code approch. Did not work.
Here is the Array
var userData = [
{
"key" : "user A",
"value" : [
{
"id" : 1,
"location" : "NYC"
},
{
"id" : 2,
"profession": "programmer"
}
]
}
]
JS:
userData.value.push({"age" : 25})
I want to add the age afte id 2 object
You have to access the first element of userData:
userData[0].value.push({"age" : 25})
you use userdata[0] to push value at last in array object
userData[0].value.push({"age" : 25})
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 6 years ago.
Improve this question
I have an javascript array of objects as follows :
list = [
{
"employee_work_rights_id":74,
"amazon_id":173,
"employee_id":3,
"work_rights":"australian_citizen",
"document_type":"password",
"display_name":"a.pdf",
"filename":"abc.pdf",
"s3bucket":"xyz",
"filepath":"employer27\/employee3\/"
},
{
"employee_work_rights_id":75,
"amazon_id":175,
"employee_id":3,
"work_rights":"australian_citizen",
"document_type":"password",
"display_name":"a.pdf",
"filename":"xyz.pdf",
"s3bucket":"zyx",
"filepath":"employer27\/employee3\/"
}
]
I tried to access amazon_id as follows :
console.log(list[0].amazon_id);
This is giving me undefined. How can I access this ?
You're doing the right thing. The way you initialize list is correct, and so is the way you access the property.
Assuming you were trying interactively in the console, the undefined you see is not the value of list[0].amazon_id. It's the return value of console.log. In Javascript, everything has a return value. However, in the console, just above or just below your undefined, you should see amazon_id's proper value.