1 00:00:00,260 --> 00:00:02,560 You may have heard of JSON before. 2 00:00:02,560 --> 00:00:04,580 No, I'm not talking about my name. 3 00:00:04,580 --> 00:00:06,520 I'm talking about the serialization format. 4 00:00:07,600 --> 00:00:11,200 JSON stands for JavaScript Object Notation and 5 00:00:11,200 --> 00:00:14,470 is a way of serializing data so that you can access it later. 6 00:00:15,930 --> 00:00:19,410 Why are we talking about this in Ruby when it's JavaScript? 7 00:00:19,410 --> 00:00:21,365 I'm so glad you asked. 8 00:00:21,365 --> 00:00:24,520 JSON is a plain text serialization format, 9 00:00:24,520 --> 00:00:29,250 which means you don't really need any special software to read or write it. 10 00:00:29,250 --> 00:00:33,055 You only need to be prepared for the different formats of objects inside it. 11 00:00:33,055 --> 00:00:38,910 JSON may have started with JavaScript, but it's in wide use now. 12 00:00:38,910 --> 00:00:42,550 And it's pretty much standard that every programming language can read or 13 00:00:42,550 --> 00:00:44,460 write JSON. 14 00:00:44,460 --> 00:00:49,580 Let's take a look at reading, writing, and working with JSON now using Workspaces. 15 00:00:51,130 --> 00:00:57,400 >> Okay, so let's go ahead and take a look at how we can use JSON in our Ruby code. 16 00:00:57,400 --> 00:01:01,040 So first, here is the JSON documentation and 17 00:01:01,040 --> 00:01:03,560 you can find that in notes right below the video. 18 00:01:03,560 --> 00:01:06,433 Now, what we need to do is require it, and 19 00:01:06,433 --> 00:01:10,350 then we can send in a string to the parse method. 20 00:01:10,350 --> 00:01:13,530 And that should return an object for us. 21 00:01:13,530 --> 00:01:15,400 So, let's go ahead and see how that works. 22 00:01:15,400 --> 00:01:17,420 I'm gonna load up IRB. 23 00:01:17,420 --> 00:01:21,160 And first, I need to require JSON. 24 00:01:21,160 --> 00:01:23,540 I'm gonna create a string, here. 25 00:01:26,120 --> 00:01:31,278 And we'll say that this string has a key 26 00:01:31,278 --> 00:01:37,842 with the name Jason, and location of Treehouse, 27 00:01:37,842 --> 00:01:43,360 and the year, which is 2015. 28 00:01:43,360 --> 00:01:45,620 Okay, so we've got our case and json_string. 29 00:01:45,620 --> 00:01:50,190 Now, if we wanted to turn that into a Ruby object, we could use that parse method. 30 00:01:50,190 --> 00:01:54,650 So we could say JSON.parse, and then send in the string. 31 00:01:56,240 --> 00:02:00,830 And we get back a Ruby object with everything correctly parsed. 32 00:02:00,830 --> 00:02:04,620 It's got these hash keys for name, location, and year. 33 00:02:04,620 --> 00:02:07,000 And we could assign this to a variable if we wanted to. 34 00:02:12,840 --> 00:02:18,130 And this goes from string to a hash. 35 00:02:18,130 --> 00:02:22,230 Similarly, if we had another hash. 36 00:02:22,230 --> 00:02:29,410 Let's say my_hash is name Jason. 37 00:02:29,410 --> 00:02:35,895 Now this is a Ruby hash, email jason@teamtreehouse.com. 38 00:02:35,895 --> 00:02:41,361 And we'll say favorite numbers the array 39 00:02:41,361 --> 00:02:45,900 with the numbers 1, 2, and 3. 40 00:02:45,900 --> 00:02:50,770 So we've got this hash and if we want to turn that into 41 00:02:50,770 --> 00:02:55,760 a JSON object, we can use JSON.dump(my_hash). 42 00:02:55,760 --> 00:03:01,580 And now we get back a JSON string representation of our hash. 43 00:03:01,580 --> 00:03:03,440 And this could be converted back as well. 44 00:03:03,440 --> 00:03:11,000 So we could say, json_output = JSON.dump(my_hash). 45 00:03:11,000 --> 00:03:12,410 Now we know that this is a string. 46 00:03:15,240 --> 00:03:18,560 By asking it what class it is, okay, we know it's a string. 47 00:03:18,560 --> 00:03:23,077 Now we could parse this just like other JSON, JSON.parse. 48 00:03:23,077 --> 00:03:25,895 That output, would help if I spelled it correctly. 49 00:03:28,775 --> 00:03:33,570 And we get back a Ruby hash based on that string. 50 00:03:33,570 --> 00:03:38,680 And it even has an array as one of the members. 51 00:03:38,680 --> 00:03:42,200 There's one other useful method with JSON, that we can use. 52 00:03:42,200 --> 00:03:44,640 And that is the load method. 53 00:03:45,650 --> 00:03:51,790 We can load JSON from a source, which could either be a string or a file. 54 00:03:51,790 --> 00:03:55,040 So I've prepared an example JSON file here. 55 00:03:55,040 --> 00:04:01,640 That just has that original JSON string in a file, and we can do JSON.load. 56 00:04:01,640 --> 00:04:04,550 Now, remember we have to give this a file so we'll just say file.new. 57 00:04:04,550 --> 00:04:10,130 And the path which is example.json. 58 00:04:10,130 --> 00:04:12,270 And this returns our Ruby hash. 59 00:04:13,270 --> 00:04:15,440 Now one thing to keep in mind when you are parsing JSON. 60 00:04:15,440 --> 00:04:19,440 This is going directly into Ruby. 61 00:04:19,440 --> 00:04:24,020 So, if you're going to use the load method, make sure that the data is trusted 62 00:04:24,020 --> 00:04:26,420 and clean, or there could be security concerns. 63 00:04:28,160 --> 00:04:31,540 Try working with JSON or your own now, using Workspaces.