I'm trying to access data within a json file using nodeJS
When I run this I get the error : TypeError: Cannot read property 'postcode' of undefined. Any Suggestions?
{
"apiName": "Restaurants",
"pages": [
{
"pageUrl": "https://url",
"results": [
{
"address": "3F Belvedere Road Coutry Hall, London, SE17GQ",
"phone": "+442076339309",
"name": "Troia",
"postcode": "SE17GQ"
}
]
}
]
}
var myData = require('./jsonFile.json');
console.log(myData.pages.result.postcode);
Try to access data as below:
console.log(myData.pages[0].results[0].postcode);
The value in the bracket is the index of element to access.
Its the common singular/plural trap, I fall for it all the time.
In your json, pages & results are arrays. You need to access these with an index. Also, you have a typo in the name.
Try this:
console.log(myData.pages[0].results[0].postcode);
This will gave you correct answer.
console.log(myData.pages[0].results[0].postcode);
Related
I have JSON object and I want to access "content" which I have highlighted in the s2 snapshot so currently this is the situation "content": "". So if I place some div or some anchor tag in the content something like that "content": " " So How to access this url i.e href . Any help will be highly appreciated.
usually people recieve a json object in some type of request to an API
a json object literal is already a javascript object so you'd just assign it to a variable and access it like you would any js obj
watch out for the nested array as well
var obj = yourExampleJsonObject
console.log(obj.value[0].body.content)
also please don't post code as an image in the future. Cheers!
var obj = {
"key": "value",
"array": [{
"id": "123456",
"body": {
"content": "<tag></tag>"
}
}],
}
console.log(obj.array[0].body.content);
I am trying to display some data to the view in my Angular 2 app via string interpolation. For most of the page that's working, but I'm getting an error in this one section that's not making sense to me. It looks like it should work, but I'm getting an 'undefined' error.
Here's what the data looks like that I'm accessing (note: "contacts" is on the root of the document):
"contacts": [
{
"contact": {
"_id": "111111111",
"email": [
"person#email.com"
],
"name": {
"first": "Person",
"last": "Lastname"
}
}
]
I am trying to access the email value in my template using this:
First I tried accessing the email address like this:
{{contacts[0]?.contact?.email[0]}}
But got this error:
Cannot read property '0' of undefined
So then I tried a named index:
{{contacts['contact']?.email[0]}}
This is also not working. I get this error:
Cannot read property 'contact' of undefined
And this is for a record where I know there is a value there. So I'm unclear why this isn't working? Anything look wrong about how I'm accessing the values here?
Contacts is an array, you should access with index
{{contacts[0]?.contact?.email[0]}}
if not use ngFor
<li *ngFor="let conctact of contacts">
{{ conctact.email[0]}}
</li>
I am currently working on a Javascript project where I have to parse tons of data. The project requires that I parse some JSON data and bring specific data into another array. Right now, I am using the JSON.stringify method and I can console.log all of the data that I need. The data looks something like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-85.3865810000125,
33.90171899971196
],
[
-85.38659500025622,
33.9017919996593
],
yes, this is not all that data-it is about 1200 pages long! I only pasted in the top segment of it. All I really need is how to get to the coordinates aspect of it and into an array. So what I am currently doing is this:
var work = JSON.stringify(response, null, 4)
console.log(work);
Which gives me the above response. However, I am not that familiar with JSON.stringify so if I do:
console.log(work.type);
Attempting to see what the value is for type, I get a response of undefined. Now if I try doing this:
var work = JSON.parse(response)
console.log(work);
I get a response of: VM296:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
Thus, I cannot get the data parsed which I want to get to which will be the coordinates data. Any help with using stringify would great help me. I have read a lot about how it turns the data into a string but have not really seen a lot about how to parse it. Do I have to use parse? Thank you for your help!
You dont' have to use stringify or parse for this. The response object you are receiving is already a javascript object so you can reference its properties normally
response.type; // => "FeatureCollection"
If you try and stringify the response it will lose its type property, for instance:
typeof "some string".someProperty; // => undefined
I'm trying to use Symphony CMS to output its XSL as JSON, for use in the angular phonecat app example from the tutorial. My list view json is output as an array with [{ }] brackets around it. My detail view json is output with only { } around it:
{
"position": 1,
"order": 3,
"total": "1",
"id": "sunt",
"sym_id": "21",
"cat": "back-end",
"imageUrl": "http://localhost:8080/workspace/images/phones/Nondell-streak-7.0.jpg",
"name": "Sunt",
"done": "No",
"priority": "medium",
"date-created" : "18 November 2015",
"date-modified" : "19/11/15 10:41am",
"date-due" : "18/11/15 2:13pm",
"snippet": "Quam nihil molestiae"
}
When loading my page the list view loads. When I click on an item the detail view tries to load but I get: SyntaxError: Unexpected token ,. Why? The syntax seems normal to me?
A big problem I am struggling with is that my json is created dynamically with the CMS but no actual files are on the server as far as I know.
Before I was getting the error: Error in resource configuration for action get. Expected response to contain an object but got an array so I made some changes so the list is output as an array, and the single views are output as an object.
My json is generated on localhost:8080/json/{parameter}. The param is 'todos' when not set. So my list view is situated at localhost:8080/json/todos. The param takes the selected object's name, so my detail view would be at localhost:8080/json/foo when I press the link generated by angular.
I hope my question is complete and makes sense :D
I got into a little thing while getting a JSON file from a server, the JSON file contains something like this:
var reviews = [
{
"firstName":"Simon",
"lastName":"Lock",
"fullName":"Simon Lock",
"location":"San Francisco",
"reviewTitle":"Super quality.. I will show here again!",
"reviewBody":"Super nice quality, fast devilery, good prices. I will shop here again!",
"starRating":"5"
},
{
"firstName":"John",
"lastName":"Smith",
"fullName":"John Smith",
"location":"New York",
"reviewTitle":"Princely Sum",
"reviewBody":"A decent local curry house in Faversham, Kent known for its Elvis nights.",
"starRating":"4"
},
];
And as expected, I get an error while getting the file with $.ajax:
SyntaxError: Unexpected token v
Since is not a valid JSON format, but I did also try to do it with another kind of dataType.
My question is:
Is there any way to get that variable in JSON file stored in another variable so it can be manipulated normally?
also I did try to use FileReader() but I didn't work for me.
thanks
JSON is just a way of storing data, not JavaScript code. it doesn't know what to do with var reviews =. All you store in your JSON file is the data, like below.
[
{
"firstName":"Simon",
"lastName":"Lock",
"fullName":"Simon Lock",
"location":"San Francisco",
"reviewTitle":"Super quality.. I will show here again!",
"reviewBody":"Super nice quality, fast devilery, good prices. I will shop here again!",
"starRating":"5"
},
{
"firstName":"John",
"lastName":"Smith",
"fullName":"John Smith",
"location":"New York",
"reviewTitle":"Princely Sum",
"reviewBody":"A decent local curry house in Faversham, Kent known for its Elvis nights.",
"starRating":"4"
}
]
Bear in mind that this is not something I recommend in cases where security is critical, but you do have the option of using the eval() function on the plain text of the file.
eval('var reviews = [
{
"firstName":"Simon",
"lastName":"Lock",
"fullName":"Simon Lock",
"location":"San Francisco", ...
would give you a variable reviews that contains the object you want.
edit: yes eval is evil, I know