Transcript for:
Understanding YQ for YAML Parsing

foreign we'll be taking a new topic actually about how to park yaml file and why are we doing this topic because I've received a lot of requests about how to do you know yq templating now what is yq templating how to read a yaml file because nowadays in devops world the configuration is path by yaml files only is how to read yaml files using MyQ and to make our life easy to do any kind of you know development or modifications using python or non-python so just to Quick a recap what we have done this in the same playlist you cannot subscribe my Channel please do that here so that you can get similar kind of videos from the channel itself so what we have done so far we have seen how to read and write yaml files using python how to you know insert data into my SQL database using python so now today we'll be talking about a simple quick installation of IQ binary or two one two box and onto the windows box and then we'll see quick example of how to read raml files and how to create variables out of it so that it can be passed onto some other you know as your pipeline or some other connected objects if needed so yeah there's a quick introduction so basically we are we have three kind of data into yaml files it can be a key value pairs or it can be errors and it can be dictionaries we'll see one wire so key value is nothing that uh you know left-handed it's a key and the right hand side is the value so you can see the pi value of pi is 3.14 the value of dou is a d r or female deer okay second kind of objects that you find in yaml is it's you know list or arrays now as as the name goes any other collection of similar objects so you can see India and the tables array we have one table one array and table two array similarly we have this calling words array we have four you know words name is an array now the third and final thing is dictionary then map dictionary the maps are simple you can see a collection of key value players collection of key value players within one dictionary I mean within one variable and or you can say a map so you can say Xmas 50 is a map which contains three key value Pairs and another another dictionary which is nothing but partridges and having two key value pairs in this so we can say this dictionary has certain key values and another dictionary these are the primarily three things that we talk about into ml file and in this story revolves around these three only if you understand these three then life would be easy to understand yaml file and do uh reading ml files or parsing ml file using yq now we'll see a little bit you know in Practical example so that you can understand it a better way so this is a GitHub page that I've created for this yq templating we'll see quickly uh you know what what is all there in this your page we'll come on to the vs code again so first of all we need to install this you know yq it's simple uh you know binary written in go so you can if you are working on Ubuntu box you can simply run it using blue if you are not using Ubuntu box you can using install directly the binary I mean the directly of the binary from wget on this URL since I'm already having in a blue held run this and it will just you know install the yq and it will add to the path variable so that it is up and running form it is already there so I'm good so I'll just do a yq and then help right perfect now we'll see how to read yaml files using this yq now if you're using not using Ubuntu but you want to install into Windows system that is also easy you need to either download this exe or download this file and add the exe path to a environment path variable and how to add this EnV path variable you can simply come here I'm talking about the windows environment variable click on path edit and then add new like you know New Path here and then simply click ok ok that's it perfect now this is all about documentation a lot of documentation happens quickly see the example so this is my first channel file I'm just taking the same yaml file which is there into this playlist and do the same GitHub so this is my first ml file and there are multiple things like it has a key value Pairs and it is a arrays analyst enter the dictionary Also let's see how it works so I'll go to the uh like this window box I'm into this okay so I'll do yq and then this is the starting codes let me double get the first name from the CML file so I'll do first name and then first automl first name is coming out to be null and everything why okay I need to create a string so let me run this command again I think I was running into long prong file so this is my ml file I'll just do a yq both name so I want to get the first name from the CML for the key value here so that's still I'll split on book page and then suppose I want to get the list of this you know variable so what I'll do yq dotting single quotes and then I'll just do a calling words now I want to show one quick thing about the IQ calling bird okay to my bad okay yeah fine so you can see these are the values that are written by this so you know command now I want to it's the arraylist so what I want to get the actual value so what I'll do I'll just simply do this and I'll get the result now because getting the result from a arraylist now let's see the example of this dictionary map okay I'll do yq and then I'll do this quick and then yeah now what I'm running I'm just trying to get the details under underneath this Xmas 50 variables this is a dictionary I want to get the value of this count so what I'll do I'll just do one more level I'll I will come here now I'm able to get the content location so if I do this location appearing so suppose I want to connect this variable and store it to a variable which need to be you know using some something else you know suppose into my your pipeline or something so I'll do I'll do something like this and then I'll do Eco where now I can use a variable which is being passed from a yaml file and then click it accordingly so let's see another example which is this nothing but the second dot yaml suppose I want to see the database strips or entire data structures what I'll do yq data base second automl I can see the complete data fracture detail okay now suppose I want to see the credentials of this database so I'll come here again and do credentials and then this okay actually I can get the different tables on this so let me just run the tables not tables is a array okay database and then tables see now it's coming out to be a array with two objects so not let me first of all get the value so if I do something like now let me show you one quick example uh if I do something like this name then it will give you an error because it is it is an array it is saying cannot index array with name so we need to index an array with the with DVD proper syntax okay so what I'll do the same thing but first of all I'll say this is my array perfect now in this array give me the name now you can see the enemy coming properly again if I go inside this community columns column is again another array okay array within an array so I'll do one more thing array within array okay now these are all values that are coming from these dependent columns so this is how you need to you know communicate with yq with the different yaml files and you can have any kind of complex EML files also yeah this is just an idea how to read yaml files using yq hope it was little bit you know understandable yeah it will take some time to get into your head but yeah if you understand this is a yaml bar thing and there are a lot of complex available but first of all you need to understand how your yq work how can you part a simple yaml and a complex yaml using different you know commands and then you can do any or any of your work now in similar lines if you want to get more details about yq you can do one more thing yq GitHub or you simply type yq plus the first page that's coming to your screen is the official yq open codes project that is being developed by uh I think a hundred percent and then you can see a number of examples also just to start with development you can simply go ahead and check out uh this you know page and then do certain examples so I'll just you know maybe I'll stop this video here uh please comment to the comment section if you want to understand more about yq and what are the other challenges that you face when you work with yaml files uh parsing and yq can be worked with a Json file to also but for now let's keep it simple with yaml file because yaml file is used everywhere whether it is your pipeline Ubuntu world or anywhere so give it a shot put me put put comments in this extension if you have any kind of doubt and queries do not forget to like And subscribe the channel for more similar kind of videos so yeah quick recap okay what we did we just simply run a couple of examples using yq yeah so I would you know say let's stop it here uh see you in the next video thank you bye