That's not a JSON Object, that's a string. The \ character is used to escape the double quote ", otherwise JavaScript would interpret the double quote as the end of the string and would throw a parsing error.
If you want to access the string as an object, you need to parse it using JSON.parse:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
const locationString = obj.data.Location;
const locationObject = JSON.parse(locationString);
console.log(locationObject[0].id);
If you don't parse it, instead, obj.data.Location is just a string:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
console.log(typeof obj.data.Location);
You usually need to stringify or parse objects for serializations purpose.
Answer from Christian Vincenzo Traina on Stack OverflowThat's not a JSON Object, that's a string. The \ character is used to escape the double quote ", otherwise JavaScript would interpret the double quote as the end of the string and would throw a parsing error.
If you want to access the string as an object, you need to parse it using JSON.parse:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
const locationString = obj.data.Location;
const locationObject = JSON.parse(locationString);
console.log(locationObject[0].id);
If you don't parse it, instead, obj.data.Location is just a string:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
console.log(typeof obj.data.Location);
You usually need to stringify or parse objects for serializations purpose.
It is Array of object of type Location in serialized form.
var response = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
}
You have to parse it.
var locationData : Array<Location> = JSON.Parse(response.data.Location);
Can someone please explain JSON to me?
ELI5 what a JSON file is
JSON is a convention or protocol for formatting and structuring data you want to store. The basic idea is that information and data is very important just in general for just about anything you might possibly might want to do, which means being able to easily access and manipulate the data you own is also very important.
When you have information (demographic data, some representation of a CAD model, numbers computed from research, whatever), you could just take that data, chuck it into a text file, and write some custom code to try and extract and write to that file in some manner. This however is somewhat of a fragile solution since anybody else who wants to work with your data would also need to write some custom code to parse however your data is formatted.
This is obviously sub-optimal -- being able to freely exchange and work with data is nearly as important as the data itself.
So, what people did was invent standards for how data should be formatted and structured (and took care to make sure those standards were flexible enough to represent all kinds of data). The idea is then that if somebody creates a library that can understand and work with that format, everybody else can use that library without having to constantly reinvent things. As they say, don't reinvent the wheel.
XML is one such standard. CSV is another. JSON is yet another. There are many.
JSON, in particular, was designed to be simple and minimalistic. The rules for what constitutes a valid chunk of JSON were designed to very basic and straightforward, and to closely mimic data structures and primitive types that many programming languages have built-in by default.
For example, here is a basic example of some invented data I came up with structured in JSON format:
{
"data-type": "posts",
"posts": [
{
"title": "Comment 1",
"body": "..."
},
{
"title": "Comment 2",
"body": "..."
}
]
}If you have any experience working with languages like JavaScript or Python, you'll notice this looks identical to dict, list, and string literals. This resemblance is identical.
For comparison is how you might decide to structure this same data in a different format, XML:
<data type="posts">
<comment>
<title>Comment 1</title>
<body>...</body>
</comment>
<comment>
<title>Comment 2</title>
<body>...</body>
</comment>
</data>Learning JSON (or XML or any data exchange format) is not typically the hard part and is typically easily learned and mastered. Working with the underlying data as well as tools that can manipulate your data, store it into various forms may potentially be hard.
More on reddit.comAdding '.json' onto the end of most Reddit URLs turns it into a mini-API, i.e. reddit.com/r/javascript.json; Great for prototyping!
Simple API mocker
I made this a while a go to use for testing and while developing. You get your own endpoint and choose what to respond. (JSON)
More on reddit.comr/json
Videos
That's not a JSON Object, that's a string. The \ character is used to escape the double quote ", otherwise JavaScript would interpret the double quote as the end of the string and would throw a parsing error.
If you want to access the string as an object, you need to parse it using JSON.parse:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
const locationString = obj.data.Location;
const locationObject = JSON.parse(locationString);
console.log(locationObject[0].id);
If you don't parse it, instead, obj.data.Location is just a string:
const obj = {
"data": {
"Location": "[{\"id\":\"asdiqwe321\",\"name\":\"Manila\",\"lat\":25.42952,\"long\":-96.7960712,\"rating\":3,\"address\":\"Manila Ph\"},{\"place_id\":\"zzxdasdqwe1235as\",\"name\":\"Quezon City Ph.\",\"lat\":12.523562,\"long\":24.663415,\"rating\":1,\"address\":\"Quezon City Ph\"},{\"place_id\":\"rtiDFSDQ1321\",\"name\":\"Makati\",\"lat\":32.151236,\"long\":21.24124561,\"rating\":3.5,\"address\":\"Makati PH\"}]"
}
};
console.log(typeof obj.data.Location);
You usually need to stringify or parse objects for serializations purpose.
Answer from Christian Vincenzo Traina on Stack OverflowI've never worked with any DB or back-end, etc stuff, but I am in need of some sort of data storage. I'm working on a javascript application that will only be run on my pc, offline, and I need to be able to save information. I don't want to rely on localStorage because if the browser history is wiped then all the data goes with it.
While searching for a way to collect and store info, I read about JSON, and it sounded like what I was looking for--and yet I've spent the last 4 hours watching tutorials and so far all I know about it is it's fching JS. I sat through a 12 minute video where all the guy did was write out an object in json and then copy and paste into a js file and said "now you know how to use json in all your future projects" 🙄 like what in ACTUAL fk. You could have just WROTE that in js. What's the point of JSON? Everything I've seen or read is practically just the same as this video.
DOES json collect and store data?
Like, if I put an input form in my app, and type a name and hit submit, can I make that Input hardcode into the json file to be saved forevermore and called upon when I needed in this app? Because that's what I need. Any explanation or help on this would be GREATLY appreciated.