1 00:00:01,200 --> 00:00:05,300 Friends, here we are going to work with the Json files using Python. 2 00:00:06,500 --> 00:00:07,400 What is Json? 3 00:00:08,100 --> 00:00:11,500 You know Json is a JavaScript Object Notation, 4 00:00:11,500 --> 00:00:18,100 and it is a popular data format used for representing structured data. 5 00:00:18,100 --> 00:00:20,300 [no audio] 6 00:00:20,300 --> 00:00:22,500 Guys before Json, you know XML, right? 7 00:00:22,600 --> 00:00:23,900 What is the purpose of XML? 8 00:00:24,700 --> 00:00:29,000 The purpose of XML is just to transfer your data from server to 9 00:00:29,000 --> 00:00:34,100 application, or from application to server, or simply server to client or 10 00:00:34,100 --> 00:00:36,700 client to server. The same way 11 00:00:36,700 --> 00:00:38,500 we are using even Json as well. 12 00:00:38,500 --> 00:00:40,300 [no audio] 13 00:00:40,300 --> 00:00:41,300 Right? 14 00:00:41,500 --> 00:00:46,500 See let me show you a simple Json file. 15 00:00:46,500 --> 00:00:50,100 [no audio] 16 00:00:50,100 --> 00:00:52,400 So, we have a simple Json file, let me open it. 17 00:00:52,400 --> 00:00:53,400 Yeah, see here. 18 00:00:54,400 --> 00:00:59,700 This entire thing is one Json file and if you observe 19 00:00:59,700 --> 00:01:05,300 in that you have curly brace starting and ending. So in terms of Python, 20 00:01:05,300 --> 00:01:09,700 this is a dictionary, but as per your Json, this is a object. 21 00:01:09,700 --> 00:01:14,910 You should say your dictionary as object in your Json. 22 00:01:14,900 --> 00:01:19,510 Fine. See, for the same Json file, whatever the content we represented, 23 00:01:19,500 --> 00:01:24,800 I mean whatever they represented using this Json format, same data 24 00:01:24,800 --> 00:01:26,800 they're also going to represent using XML. 25 00:01:26,800 --> 00:01:31,500 That means you can write your data to transmit over the network 26 00:01:31,500 --> 00:01:37,900 either by using XML or by using Json. For simpler data 27 00:01:37,900 --> 00:01:41,500 we are going to use Json format, for complexity data 28 00:01:41,500 --> 00:01:43,200 we are going to use XML file. 29 00:01:43,200 --> 00:01:45,800 [no audio] 30 00:01:45,800 --> 00:01:49,800 Now, let me copy this simple Json file and I want to store it as some, 31 00:01:51,200 --> 00:01:53,500 with some Json file, let's say 'myjson' 32 00:01:53,500 --> 00:01:55,700 [no audio] 33 00:01:55,700 --> 00:01:57,000 '.json' extension. 34 00:01:58,100 --> 00:02:00,000 So, this is now your Json file. 35 00:02:00,800 --> 00:02:06,200 So, what we are doing here is we want to read the Json file or 36 00:02:06,200 --> 00:02:10,300 if we want to write the content to your Json file how we can do it? 37 00:02:11,000 --> 00:02:14,000 First, let me go with how to read a Json file. 38 00:02:14,400 --> 00:02:19,900 So I am going to store, I'm going to save program or script name as 39 00:02:19,900 --> 00:02:23,100 working with the, yeah working with Json files. 40 00:02:23,100 --> 00:02:25,400 [no audio] 41 00:02:25,400 --> 00:02:29,200 Right? See guys, whenever you want to work with the Json, first 42 00:02:29,200 --> 00:02:34,400 of all you need to import a Json module, which is a default module then 43 00:02:35,500 --> 00:02:38,709 let me take 'file', or 'req_file', 44 00:02:38,700 --> 00:02:40,000 what is the file you want to read? 45 00:02:40,800 --> 00:02:44,800 So guys, you know, it is a better practice to provide complete path, 46 00:02:44,800 --> 00:02:49,500 so as of now directly I'm giving 'myjson.json' file name itself 47 00:02:49,500 --> 00:02:53,000 without giving complete path because that Json file 48 00:02:53,000 --> 00:02:55,600 and my script both are there in the same location. 49 00:02:56,500 --> 00:02:59,500 But anyway, for better practice, please, please provide complete 50 00:02:59,500 --> 00:03:02,500 path for your file whenever you are writing Python scripts. 51 00:03:03,500 --> 00:03:10,200 That's fine. Now guys, generally if we want to read a file, if you want to 52 00:03:10,200 --> 00:03:15,200 read the content of a file what are our steps? Open that file 53 00:03:15,200 --> 00:03:19,900 in 'read' mode, that is the first step. Open that file in 'read' mode. 54 00:03:19,900 --> 00:03:23,100 You know even if you don't mention any mode in open 55 00:03:23,100 --> 00:03:27,200 function, by default that is the 'read' mode. But if you want to mention, 56 00:03:27,200 --> 00:03:29,200 yes you can mention it, 'read' mode. 57 00:03:30,100 --> 00:03:33,000 Then finally, we are closing. Between these two 58 00:03:33,000 --> 00:03:34,900 whatever the action you want to perform 59 00:03:34,900 --> 00:03:38,600 you can perform. See this is not a normal text file. 60 00:03:38,600 --> 00:03:45,400 If it is like a normal text file directly we can print 'fo.read'. If it is normal 61 00:03:47,000 --> 00:03:51,800 text file we can use this one, right? You are getting 62 00:03:51,800 --> 00:03:54,200 but you are getting like, again this 63 00:03:54,200 --> 00:03:59,609 as a Json only. But what we need? We want to work with the Json file, 64 00:03:59,600 --> 00:04:02,500 that means you need to convert that data into Python 65 00:04:02,500 --> 00:04:05,800 understandable data. Now, again, this is like a string. 66 00:04:06,500 --> 00:04:08,700 This is just your Json object as it is. 67 00:04:09,800 --> 00:04:14,200 But, we don't want that Json object because if you want to modify 68 00:04:14,200 --> 00:04:17,800 if you want to get, suppose, let's say based on keys, 69 00:04:17,800 --> 00:04:21,200 if it is dictionary based on this key you can get this value, 70 00:04:21,200 --> 00:04:25,300 but now this is not a dictionary, then you can't to do some 71 00:04:25,300 --> 00:04:32,100 process on this data like, you know key-value operations 72 00:04:32,100 --> 00:04:34,100 on your data because that is not a dictionary. 73 00:04:35,400 --> 00:04:40,010 What happened? One, two, one more parentheses. 74 00:04:40,000 --> 00:04:44,900 Yeah. See, that is a string. You're able to read your data, your Json data, 75 00:04:44,900 --> 00:04:49,800 but that is a string itself. But suppose for seeing 76 00:04:49,800 --> 00:04:52,600 purpose how you are getting that data? As a dictionary, but that is not 77 00:04:52,600 --> 00:04:58,100 a dictionary. How you can process that? We can't process that. 78 00:04:59,100 --> 00:05:03,909 So now, what I am doing is I am going to convert this into your Python data. 79 00:05:03,900 --> 00:05:08,800 So, for that what you have to do is simply your data, 80 00:05:08,800 --> 00:05:13,900 directly you can print it. 'print', use 'json.load' 81 00:05:14,000 --> 00:05:19,000 You have to load from your Json. Simply if you parse your content 82 00:05:19,000 --> 00:05:23,000 your file object to your Json object, Json module 83 00:05:23,300 --> 00:05:27,010 Json will convert your data into Python understandable data, 84 00:05:27,000 --> 00:05:31,100 that is simply dictionary format. Now see the result. Now from 85 00:05:31,100 --> 00:05:34,600 this data I can take simply 'get', because there is a dictionary, 86 00:05:34,600 --> 00:05:41,400 right, I can do some operation called, '.get'. Now see the result. 87 00:05:41,900 --> 00:05:43,610 Yes, you are getting something like that. 88 00:05:43,600 --> 00:05:46,600 Now I can do one more operation because this is also dictionary but 89 00:05:46,600 --> 00:05:50,000 previously with 'fo.read' you are not getting your data as a 90 00:05:50,000 --> 00:05:52,000 dictionary, you're getting something like a string. 91 00:05:52,000 --> 00:05:54,700 So, on that string you cannot perform your dictionary 92 00:05:54,700 --> 00:05:57,600 operations like key value operations. 93 00:05:58,400 --> 00:06:04,409 That's why we are converting the Json data in terms of dictionary. For that 94 00:06:04,400 --> 00:06:08,200 we have a simple syntax called, 'json.load', your file object. 95 00:06:08,200 --> 00:06:09,600 That's it. Very simple. 96 00:06:09,600 --> 00:06:11,200 [no audio] 97 00:06:11,200 --> 00:06:16,900 Right? See, if you want to print your Json data as it is you can also do this one. 98 00:06:16,900 --> 00:06:19,200 [no audio] 99 00:06:19,200 --> 00:06:22,200 but don't execute those sixth and seventh lines at a time. 100 00:06:22,200 --> 00:06:24,100 You have to execute anyone of that, 101 00:06:24,100 --> 00:06:26,400 because once if you read the content you don't have anything to 102 00:06:26,400 --> 00:06:27,400 read again in that. 103 00:06:27,400 --> 00:06:29,400 [no audio] 104 00:06:29,400 --> 00:06:31,100 Right? Okay, fine. 105 00:06:31,100 --> 00:06:33,400 [no audio] 106 00:06:33,400 --> 00:06:39,100 Now this is simple way just to read your data. Now what I want to do is in case 107 00:06:39,100 --> 00:06:42,810 in my hand if I have some data how can I store that data into 108 00:06:42,800 --> 00:06:46,909 a Json file? So before going to store into your Json file 109 00:06:46,900 --> 00:06:50,210 you need to think about what type of data you have in your 110 00:06:50,200 --> 00:06:54,510 hand. You know while reading the data from your Json what 111 00:06:54,500 --> 00:06:58,110 is the format you are getting? Dictionary format, that's why 112 00:06:58,100 --> 00:07:01,310 whenever if you want to store your data into a dictionary, 113 00:07:01,300 --> 00:07:07,900 into a Json file you should have in your hand data as a dictionary, right? 114 00:07:08,100 --> 00:07:11,100 See let me do one thing. I'm going to create a 'my_dictionary'. 115 00:07:12,100 --> 00:07:15,400 Suppose I am going to create dictionary like "Name", 116 00:07:15,500 --> 00:07:17,000 I am writing as "Narendra". 117 00:07:19,500 --> 00:07:20,600 Then I am writing "skills", 118 00:07:20,600 --> 00:07:25,000 [no audio] 119 00:07:25,000 --> 00:07:35,800 'Python', 'shell', 'yaml', 'AWS', something like that. 120 00:07:36,800 --> 00:07:40,800 Right. Now in your hand you have a dictionary data. This dictionary data 121 00:07:40,800 --> 00:07:44,000 you can able to write into your Json if you know the syntax. 122 00:07:44,000 --> 00:07:46,000 So now let me try for syntax. 123 00:07:47,300 --> 00:07:53,909 So first of all, just assume that you are going to create a Json file 124 00:07:53,900 --> 00:08:02,100 Let me take 'req_file' as, suppose 'myinfo.csv'. Sorry, 'json'. 125 00:08:03,100 --> 00:08:08,400 So, this is the Json file I want to create with the help of our Python. 126 00:08:08,400 --> 00:08:11,600 So, what I want to write in Json file? I need to write this data. 127 00:08:11,600 --> 00:08:14,000 What is your data? It should be a dictionary. 128 00:08:15,100 --> 00:08:16,500 Right? Then fine. 129 00:08:17,200 --> 00:08:22,900 Let me open first your file in 'write' mode. Whether it is Json or csv or txt file, 130 00:08:22,900 --> 00:08:26,600 the first step you have to do you have to open it in your required mode. 131 00:08:26,600 --> 00:08:29,510 I want to write the content to your file 132 00:08:29,500 --> 00:08:32,400 Then I need to open it in 'write' mode. Then finally just to close it 133 00:08:32,400 --> 00:08:36,500 between this you need to write your data into your 134 00:08:36,700 --> 00:08:41,600 Json, right? How we can write our data into Json? 135 00:08:43,200 --> 00:08:45,100 Right? Fine. See that, what I am doing. 136 00:08:45,100 --> 00:08:48,200 [no audio] 137 00:08:48,200 --> 00:08:53,600 Simply, 'json.dump'. 138 00:08:54,500 --> 00:08:58,900 Just we are pushing our data into Json file, 'json.dump', 139 00:08:59,100 --> 00:09:00,800 your dictionary data 140 00:09:00,800 --> 00:09:02,800 [no audio] 141 00:09:02,800 --> 00:09:05,400 with respect to your file object. That's it. 142 00:09:05,400 --> 00:09:07,800 Now, let me run it and see the output. 143 00:09:09,000 --> 00:09:11,600 So you just pushed your data into your Json file. 144 00:09:11,600 --> 00:09:14,100 Now, we don't have any output here, right? 145 00:09:14,100 --> 00:09:17,410 We just created a file. Again, if you want to see the data 146 00:09:17,400 --> 00:09:18,410 what is there in your dictionary 147 00:09:18,400 --> 00:09:21,100 you need to read the data, you need to read that Json file. 148 00:09:22,100 --> 00:09:27,900 So, just now we created a Json file called 'myinfo.json', let me open that file. 149 00:09:27,900 --> 00:09:34,000 [no audio] 150 00:09:34,000 --> 00:09:34,010 'myinfo' or 'myjson', what is that? 151 00:09:37,300 --> 00:09:39,900 I think 'myinfo', right? yeah. See that. 152 00:09:39,900 --> 00:09:42,400 [no audio] 153 00:09:42,400 --> 00:09:45,500 You're getting again a dictionary in a single line as of now. 154 00:09:45,400 --> 00:09:47,200 Of course, this is in a file. 155 00:09:47,200 --> 00:09:53,400 Now, you have to say that this is a Json data or simply object in your 156 00:09:53,400 --> 00:09:58,300 Json. But whenever if you see, here right, Json data 157 00:09:58,300 --> 00:10:00,900 consists of some spaces, some indentations, 158 00:10:00,900 --> 00:10:05,100 right? But that type of indentations you are not getting 159 00:10:05,100 --> 00:10:07,100 here while creating your Json file. 160 00:10:08,000 --> 00:10:09,600 It's simple you can create it. 161 00:10:10,100 --> 00:10:15,900 Nothing is there. While writing your content into your Json file you need to mention 162 00:10:15,900 --> 00:10:20,100 what is your indent. So I want to take four spaces. 163 00:10:21,100 --> 00:10:24,700 Just run it and open your file, Json file 164 00:10:24,700 --> 00:10:30,100 once again. Now you will get indentations clearly in your 165 00:10:31,100 --> 00:10:33,700 Json file. See that. Now you're getting clearly, right? 166 00:10:35,200 --> 00:10:36,200 That's it. 167 00:10:36,200 --> 00:10:38,400 [no audio] 168 00:10:38,400 --> 00:10:42,400 So, guys, this is the simple way to read content from your Json 169 00:10:42,400 --> 00:10:44,700 and to write content into your Json. 170 00:10:45,400 --> 00:10:49,800 The only thing you have to remember is reading is nothing but 171 00:10:49,800 --> 00:10:53,800 loading content from your Json. Writing is nothing but 172 00:10:53,800 --> 00:10:58,100 whatever the content you have that we are writing into your Json 173 00:10:58,100 --> 00:11:02,000 or pushing into your Json or dumping into your Json file. 174 00:11:03,300 --> 00:11:08,200 Finally, whenever if you are reading the content from your Json you are 175 00:11:08,200 --> 00:11:13,300 getting your output as a dictionary with the help of your 'json.loads', 176 00:11:14,200 --> 00:11:19,400 sorry, 'json.load', and whenever if you are trying to dump your data 177 00:11:19,400 --> 00:11:24,200 into a Json file, your data should be again a dictionary. That's it. 178 00:11:25,400 --> 00:11:27,300 Right? Fine. See here. 179 00:11:27,300 --> 00:11:33,200 Finally, we have small diagram here. In terms of Python if it is a 180 00:11:33,200 --> 00:11:35,100 dictionary, in terms of Json 181 00:11:35,100 --> 00:11:39,910 it is an object. So whatever the Python lists and tuples are there, 182 00:11:39,900 --> 00:11:46,310 they are simply Json array, string is string only. Integer, float and whatever 183 00:11:46,300 --> 00:11:49,410 it may be complex, they are numbers in your Json. 184 00:11:49,400 --> 00:11:54,510 So true, false, see that, true and false with small letters, none, null. 185 00:11:54,500 --> 00:11:57,200 So, if you remember this while working with your real-time, 186 00:11:57,200 --> 00:12:00,800 you know, those who are going to work with your AWS side 187 00:12:00,800 --> 00:12:03,710 they're going to create so many times policies. 188 00:12:03,700 --> 00:12:09,510 So policies are in terms of Json format, right? So there it will be very helpful. 189 00:12:09,500 --> 00:12:15,710 Okay. So just remember this so that you can easily understand how we can convert our 190 00:12:15,700 --> 00:12:20,300 data into Json, or from Jason to Python. Right? The main thing 191 00:12:20,300 --> 00:12:21,300 you need to remember this. 192 00:12:23,300 --> 00:12:26,600 Your Json into, your Json data into Python is always a dictionary. 193 00:12:27,200 --> 00:12:30,400 If you want to write your content into your Json, first thing, you 194 00:12:30,400 --> 00:12:33,200 should write your data in Python as a dictionary that 195 00:12:33,200 --> 00:12:36,800 data we need to dump into Json, then that will become a object, 196 00:12:36,800 --> 00:12:37,800 That's it. 197 00:12:39,200 --> 00:12:40,200 Simple, right? 198 00:12:41,800 --> 00:12:43,800 Okay guys, thank you for watching this video. 199 00:12:43,800 --> 00:12:51,700 [no audio]