Check is JSON is valid [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 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.

Related

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"
}

What does this error info. from node/express mean? [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 4 years ago.
Improve this question
I have this simple local.JSON file I use with node-config:
{
"MySQL": {
"connection": "mysql://root:foo#localhost/heroku_d093ad5a46d841c?reconnect=true",
},
"Mongoose": {
"connection": "mongodb://localhost/myapp"
}
}
However, I get this error from nodemon:
Error: Cannot parse config file:
'/Users/foo/root/repo_fave/config/local.json': SyntaxError:
Unexpected token } in JSON at position 110
Particularly what does the 110 refer to. Is it the 110th character if the file was treated as a single line of string?
There is a typo in your JSON. In the MySQL block you have:
"MySQL": {
"connection": "mysql://root:foo#localhost/heroku_d093ad5a46d841c?reconnect=true",
},
This should be:
"MySQL": {
"connection": "mysql://root:foo#localhost/heroku_d093ad5a46d841c?reconnect=true"
},
Please note the removed comma at the end of the inner line. And yes, the 110 refers to the 110th character.
PS: To figure out what problems like these are, you might be interested in JSONLint, a website to validate (and find errors in) JSON.

Traverse javascript array of objects [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 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.

get data from multidimensional json 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 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)

Categories