[MUSIC] Well, we've gotten a long way. We've learned a little about APIs that are available at the Internet. We've talked a little bit at a high level about although we haven't written a code for it yet, and immediately after we registered for our application credentials in Instagram, we saw that we were gonna get back a lot of data that we didn't recognize. That data is in a format called JSON, so in this lecture, I wanna give you an introduction to JSON, so that you know how to work with it, and how to manipulate it, in case you get data back that you would like to put into your application. At high level, we know that not everything is as deeply integrated into iOS as Facebook, Twitter, and Weibo. To use other web-based APIs, we're gonna need more tools, and one of them is to know about JSON, and how to use it, because JSON is very common data transport language. For APIs that are available on the Internet. What is JSON? Well, JSON is structured text. Which means, it's text with a syntax applied. It's text that has a particular shape, and it follows certain kinds of rules. JSON can be used to represent a huge variety of information. It enables data transport across different systems, among different languages. A different computer languages and different networks so across the internet, across WiFi, across local area networks, etcetera. JSON itself is just one of many data representations that are available out there. So, some of the other ones that compete with JSON, well JSON itself what it stands for is JavaScript Object Notation. It competes with other data representations for example, XML, you might have heard of that, which stands for Extensible or Extended Markup Language, or YAML which stands for Yet Another Markup Language, or maybe GPB which stands for Google Protocol Buffers. These are each different data representations. The first three are text based, the last one is binary. XML has really interesting theoretical properties. It has data. Google Protocol Buffers is a binary method. YAML is a lot like JSON, but it comes from a different historical place. JSON though is a very common one, and often you'll find that JSON is supported and other ones are supported as well. I want to focus on JSON though, and teach you a little bit about how that works. Even though the J-S in JSON stands for JavaScript. That just reflects its origin. It doesn't reflect how it's actually used nowadays. This format is used very widely for transporting data across the internet. And there are libraries for it in almost every language you can think of, from ASP and ActionScript or ASP, and ActionScript To Haskell to LotusScript to Prolog and Python, Visual Basic and Visual FoxPro. Who here knows what that is? Microsoft something. All these languages have libraries that support JavaScript Object Notation, or JSON, even through they're not JavaScript. JSON is the format though in which many of these web services communicate. So, if you have an app running on your phone, you may request information from a web service, and what the web service will do if the authentication is okay and the request is well formed and everything, the data will come back as a JSON response. The data will come back as text. It will come back as text with a syntax applied. It will come back as text structured in the JSON format. Instagram is going to do this. We need to understand how to work with the JSON format in order to get the information that we want from JSON. So, what does it look like? Well, on the right here is an example of some JSON, that I was using for a project, and just to give you kind of a quick sense of what you're looking at. JSON was originally designed to pass data between browsers and servers. Because browsers run JavaScript it was initially the way that, browsers alone would get data from remote servers. But what we're talking about is using an app to get data from a remote server. JSON, you may notice, has no tags. It only has data in all these different places. XML has tags. HTML has tags, JSON does not. JSON doesn't really have any meta-data either. Everything that you see in that example, with the exception of the punctuation, is data that is accessible or meant to be accessible and used by the application that's using it. Sometimes, some of these languages have additional information that's describing data itself rather than being data. So, what does JSON do? Well, JSON doesn't do anything, it's just a data format. If you wanna do anything with JSON, you have to write a program or find a program that manipulates this text. You need a program if you wanna search that text. You need a program if you wanna display that data, you need a program if you want to change that JSON data. It's just like a text file that's coming across the internet for you, but JSON isn't that text, and because of that there are many programs that can read and edit text already and so those programs can all read and edit JSON. Any text editor can do that. Many programs can read and write text and so, they are already eligible to read JSON. Although, most programs have libraries that are more sophisticated and don't treat JSON just as text. They treat it as structured text. Where did it come from? JSON was developed by a community of programmers, who thought that what was the common data transport language at the time XML that the meta-data that was in XML was unnecessary. It created files that were too big. That were too hard to maintain. That actually at the end of the day weren't that valuable. It also happens to be that JSON is the native storage format for data in JavaScript, and so just by exporting these JavaScript objects, the developers that were using it were using JSON along the way. And so, it was a way to get certain web services up and running very fast. And without a lot of the overhead that's associated with XML. If you wanna know more about it, if you wanna dig in a little bit deeper, there's a website, www.json.org that lists most of the specification and most of the protocol for JSON, including places where you can find libraries, a little bit about the history and some of the diagrams that I'm gonna show you in the next few slides. The basics of JSON are the data elements that are contained within it. You can represent strings, numbers, objects in a race. Those are all sort of higher level values that are composed of lower level data. You can have the primitive true which is treated as a token, the primitive false which is the text false treated as a token. Or the primitive null, which is the text N-U-L-L that's treated as a token of null. So, this diagram on the right shows that in order to have a value, you have to be able to pass from the left side to the right side following one of those lines in order to have a valid value. And it just reflects the same thing that's in the outline there. Now, there are two basic structures, very common structures, that are available. The first structure is an object. An object is something that maps keys and values. It's an array of key value pairs. You should think of it, if you are familiar with Java as a map, or PEARL, as an associative array. Several languages are called Dictionaries. And we've seen it in this specialization as an NSDictionary. So, an object maps keys to values. In JSON, there is also an array and array is an ordered list of values. So, you should thank a list from Java and array from objective seed or NSarray, which is the specific implementation of the array in objective seed. So, those are the 2 structures that can be used to map values together. So, the object is defined by curly braces. So, in order to have a valid object, it has to start with a curly brace, and then the key value pairs are separated by a colon. Now in JSON when it's transported across the internet, the elements, the names are always strings. And the values are always values. So, it's not being set that the values can be a string, or a number, true, false, null. But the keys are always a string. Keys and values are paired together with a colon. And then, if you have more than one key, value pair in your dictionary, then those are separated with a comma, so if you follow the graphical object there you can see that if you just have open curly brace, followed by a closed curly brace, that's a valid object. If you have an open curly brace with a string, a colon, and a value, and a closed curly brace, that's a valid object with a one key value pair. Or you can represent several key value pairs which are strings and values, if you separate them with commas. In contrast, an array is specified by a delimited by square brackets. Square brackets in which there is a sequence of values. Values again being strings, numbers, true false, null. In an array, you don't have specific keys. You just have values, and those values are ordered and they're separated by commas. At the lowest level, a string is represented as a sequence of unique code characters. The recommended representation recommended in coding is utf-8, which is standard on Macs. The strings are always delimited by double quotes. And they can have any number of UNICODE characters between double quotes. The slash is used as an escape character. So, if you want to represent a double quote, for example, within your string you need double quote, a slash double quote, and then another double quote. And that would be a string that has a single double quote in it. There are other things that you can escape, for example, the backspace key, you can do slash b, a new line is backslash n. You can also represent the Unicode characters numerically by /u, and then the four digit hexadecimal number representing that Unicode character. But aside from all of that encoding stuff. Basically, you should just think of a string a text between two double quotes. A number is a standard number including scientific notation, and so here is kind of fancy looking diagram showed you how you can move through that diagram, and still have a valid number. Some examples of valid numbers are on the left. You can have positive and negative integers, for example -1. You can have floating point numbers, for example 0.1. And you can have numbers in scientific notation. For example -1234.1234e-5. Those are all valid numbers. Notably, they're not delimited by anything. There's no quotes around them. JSON as a convention ignores any whitespace that is outside of double quotes. So, within double quotes, the whitespace is treated as data that is part of a string. But, outside of double quotes, whitespace is just used to make the file human readable, but it's not parsed or interpreted in any particular way. So now, when we look at a bit of JSON, we look at it with a little bit different eyes. So, here we see that we have a JSON array, specified because it's delimited by square brackets. And we see that there are five elements in this array. The first three elements are strings, the fourth element is a number, and the final element is the token true. So, couple things you can realize from this. An array doesn't have to all be filled with the same type. Here we have string, string, number, and then Boolean token. You could see that those five elements are separated by four commas, the strings are delimited by double quotes, and the numbers just the number one. All right, so that's a good example of an array. Here's an example of an object or a dictionary, delimited by curly brackets, there are three keys. In JSON, in a key, a dictionary or in an object, the keys are always strings. So, you see that the keys are the string place, the string activity, and the string duration, but the values are not all strings. We see the first two values are strings, the first string being at home. Notably, there's whitespace within that string separating 'at' and 'home' and that's part of the data, and in the second case it says 'watching YouTube'. You notice that there's a lot of white space between the first curly brace and the start of the double quotes before place. That's just there for readability. That's not part of the syntax, in the way that if you're familiar with Python, indentation is part of the syntax. That's not true for JSON. Here's a really important thing about JSON. You can nest these structures, and you saw that, if you recall back to the Instagram example data. So for example, here we have a dictionary at the high level. That dictionary has three keys. The string place, the string activity, and the string duration. The value that's associated with the first key place is an array. It's an array that has three strings in it. At home, at work, and at park. The second element in our outermost dictionary is the value, sorry, the second element in our outermost dictionary is the key pair activity, and then an array. That array has two elements in it, which are both strings, watching YouTube and writing code. The third element in our outermost dictionary is specified by the key duration, and the value associated with duration is an array that has four numbers in it, one, two, three, and five. And they're numbers, so they're not delimited by double quotes. In general, when you see JSON in practice, these structures get nested very deeply. And they generally make sense with whatever you're trying to do, whatever structure you're trying to work with. There are a lot of tools that are available for working with JSON, in addition to the libraries which do the reading and writing from within a program. And they're easy to find. For example, if you go to pro.jsonlint.com, that's a nice tool that allows you to cut and paste JSON into a browser. And if you hit the checkmark it'll tell you whether or not it's a well-formed JSON or not. If it's not it will give you an error message showing where the problem is. If you put some raw JSON in there, it'll also format it for you in a human-readable format with indentations. Notably, in JSON, there's no defined schema language. So, there's nothing telling you what you should, and shouldn't indent and where you should do that. There's no built in namespace's, so there's no metadata. Namespace's you've might have run into if you're familiar with XML or HTML. And, there's no built in transformation languages either. There's nothing that says, there's a specific way to translate this JSON into HTML. Sometimes, you can find examples of that with XML. Now, within a programming language, you treat this as a series of nested objects. And so, if this was your data, over on the left side, and in a general programming language, you wanted to access elements of it, you would say that this data is stored in a variable called data. And it's not stored as a string. That string has been compiled into an object. And so, if you want to access the phrase at home from that first nested component you would say data, you would access using square brackets the value associated with the key place. That would return the array that has three elements in it. And so if you wanna access the first element, you index zero into it. So data, sub place, sub zero equals at home. So that first statement is true. The second statement says I would like the value associated with the key activity. That also returns an array. That array has two elements, and the first element, the element index one, which is actually the second element, is the string writing code. So that second statement is true. The third example is asking for the value associated with the key duration. The value associated with that key duration is an array with four elements in it, and when we ask for the fourth element, by using index three, we get the value five back. So all three of those statements are true. Within Objective-C, here's an example of some code that will parse raw data, and you can use the JSON representation as a result. Now, generally speaking, you're not going to have string literals as the source of your JSON data. That data will generally be coming from a network call, we'll see that in our example. But in this case, I want you to see that I've created a command line program with a main function, and I've just defined using a string literal some raw data. What you may notice about this raw data, is that at the end of every line, there's a backslash. That is information for the Objective-C compiler to say that this string continues on the next line. Within Objective-C, if I want to represent a double quote, I have to escape it with a back slash as well. And so you see that in here also. So over on the right you see an example of a cleaned up version of a string in the gray box, and on the left I've translated that string directly into string that's being assigned to the variable raw data stream, which is appointed to an MS string. Then down two lines below you can see I'm creating a pointer to an NS data object. And I'm doing that by sending a message to the NS string object whose name to the pointer to the NS string object, whose name is raw data string. And I am sending it the message data using encoding. What that's saying is it says, I want you to turn this data string into an NS data object, and I want you to do it using the rules of UTF8 encoding. So that's a particular way of representing those characters. What you get back is an NSData object and then that NSData object is what you can turn into JSON. The next line, you can see that I'm using a class called NS JSON serialization. That's going to take some raw data. It's going to send the message JSON object with data and pass it the parameter of raw data, pass it zero as options, and pass it an error object, in case that raw data isn't valid JSON. The result that comes back is an object that has been parsed and represents the data that is present in that string. So in that final line I say, if no error occurred, so if (!error), then what I'm gonna do is I'm gonna log to the console JSON data, and I'm gonna access into it, I'm gonna use NSString of place. So what that says is, give me whatever is associated with the key place. And we know from the gray box that that is an array with three elements. And so if I ask for the first element with index zero and I print that out, you can see that I get the string at home. In the second case I ask for the same value associated with the key place but this time I want the first element. I want the second element with index one. And I get at work. In the third NSLog statement, I ask for the value associated with the key duration, and I ask for the second element in that array that has index one, and I get back the value of two. So what you've seen in this example is how that raw string gets translated into an object, whose name in this code is JSON data. And, how you can access the elements of that data by using the dictionary and array semantics that we've already learned in Objective-C. So, this is how we're going to work with JSON. Now, we know that our Instagram feed is gonna give us back JSON, and we know we're trying to get that URL, so next thing we have to do is, we have to log in to Instagram and get that data back, so that we can parse it and put it into our app. In summary, JSON is human readable text. It's a hierarchical data structure, meaning that you can nest elements within elements. It can be parsed and it can be used in many languages. We're gonna use it in Objective-C. And tools exist to read and write it in almost every platform and certainly in Objective-C. So let's go ahead and see how we're gonna get it from our Instagram API. [MUSIC]