Introduction to YAML/JSON for configuration

Introduction to YAML/JSON for configuration

1 . Introduction to Yaml

YAML is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Extensible Markup Language (XML) but has a minimal syntax that intentionally differs from Standard Generalized Markup Language (SGML). It uses both Python-style indentations to indicate nesting and a more compact format that uses [...] for lists and {...} for maps but forbids tab characters to use as indentation thus only some JSON files are valid YAML

2 . General syntax of yaml

YAML always requires colons and commas used as list separators followed by space with scalar values. Nodes should be labeled with an exclamation mark (!) or double exclamation mark (!!), followed by a string that can be expanded into a URI or URL.

3 . Uses of Yaml

One of the most common uses for YAML is to create configuration files. It's recommended that configuration files be written in YAML rather than JSON, even though they can be used interchangeably in most cases because YAML has better readability and is more user-friendly.

4. Benefits of yams

  • Simple and easy to read.

  • It has strict syntax and indentation is important.

  • Easily convertible to JSON, XML.

  • Most programming languages use yaml.

  • More powerful for representing complex data.

  • Pursing is easy that is easy to read a data.

5. DATA SERIALIZATION:-

YAML’s data serialization format includes an understated and unique advantage, its Anchors. YAML anchors and aliases let you reference and use the same data multiple times within a single YAML document. YAML’s Anchors are a time-saving advantage for a developer writing lengthy pipelines.

6. DATA DESERIALIZATION :-

Deserialization is the process of turning data from an incoming byte stream into either a network object or a data object. Therefore, deserialization is the exact opposite of serialization, and this procedure builds the objects once more regardless of whether the user wants to change the version or keep it the same as it was before. The user can make use of such objects in the programming language without having to make any further modifications to those objects.

7. Datatypes

YAML has three types of data types:

  • Scalar

    Scalar is a simple data type. In YAML, scalar means a simple value for a key. The value of the scalar can be integer, float, Boolean, and string. Scalar data types are classified into two data types:

    • Numeric Data type

      The Numeric Data type is also specified into three parts

      • Integer

      • Floating point numbers

      • Booleans

    • String

      YAML strings are Unicode. In the following example, we are going to define a simple string, without using quotes.

  • List

    We can define the list in a single line as follows:

  • Dictionary
    If we want to write a complex YAML file that holds a complex data structure, we will use dictionaries. It is a collection of key: value pairs and each of the key: value pairs can be nested with a lot of options.

8. Core Datatypes

  • Maps

  • Anchors:-

    Anchors help us in reusing properties without having to code them again and again.

9. How yaml files can help in configuration

YAML files use a .yml or .yaml extension and follow specific syntax rules.

YAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML.

There are no usual format symbols, such as braces, square brackets, closing tags, or quotation marks. And YAML files are simpler to read as they use Python-style indentation to determine the structure and indicate nesting. Tab characters are not allowed by design, to maintain portability across systems, so whitespaces—literal space characters—are used instead.

Comments can be identified with a pound or hash symbol (#). It’s always a best practice to use comments, as they describe the intention of the code. YAML does not support multi-line comments, each line needs to be suffixed with the pound character.

10. Introduction to JSON

JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values). It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers. JSON is a language-independent data format. It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data. JSON filenames use the extension .json.

11 . General syntax of JSON

12 . Uses of JSON

  • It is used while writing JavaScript-based applications that include browser extensions and websites.

  • JSON format is used for serializing and transmitting structured data over a network connection.

  • It is primarily used to transmit data between a server and web applications.

  • Web services and APIs use JSON format to provide public data.

  • It can be used with modern programming languages.

13. How JSON files can help in configuration

There are several reasons why JSON is used for configuration files. The biggest reason is probably that it is easy to implement. Many languages have JSON support in the standard library and those that don’t almost certainly have an easy-to-use JSON package readily available. Then there is the fact that developers and users are probably already familiar with JSON and don’t need to learn a new configuration format to use the product. And that’s not to mention all the existing tooling for JSON, including syntax highlighting, auto-formatting, validation tools, etc

14. Different between YAML and JSON

YAMLJSON
Comments are denoted with a hash/number sign.Comments are not allowed.
Hierarchy is denoted by using double space characters. Tab characters are not allowed.Objects and Arrays are denoted in braces and brackets.
String quotes are optional but it supports single and double quotes.Strings must be in double quotes.
Root node can be any of the valid data types.Root node must either be an array or an object.

15. Tools for configuring and validating yaml & json

ValidKube combines the best open-source tools to help ensure Kubernetes YAML best practices, hygiene & security.

yaml to json converter world's simplest yaml tool

16. Why does Kubernetes configuration build on Yaml language?

Kubernetes works based on a defined state and an actual state. Kubernetes objects represent the state of a cluster and tell Kubernetes what you want the workload to look like. Kubernetes resources, such as pods, objects, and deployments can be created using YAML files.

When creating a Kubernetes object, you’ll need to include specifications to define the object's desired state. The Kubernetes API can be used to create the object. The request to the API will include the object specifications in JSON, but most often user provides the required information to kubectl as a YAML file. Kubectl will convert the file into YAML for you when it makes the API request.

Once an object has been created and defined, Kubernetes works to make sure that the object always exists.

Developers or sysadmins specify the defined state using the YAML or JSON files they submit to the Kubernetes API. Kubernetes uses a controller to analyze the difference between the newly defined state and the actual state in the cluster.

References

🚩 https://youtu.be/5XlhLD7pyFU

🚩 https://youtu.be/5fLA-WBK49w

🚩 https://adityamca123.hashnode.dev/validate-clean-secure-your-k8s-yaml-using-validkube

That's all for this blog, I hope you will learn something new. And feel free to share your thoughts and feedback, Thanks for reading.

Feel free to reach out to me 👀

Twitter 🖱

LinkedIn 🖱

Github 🖱

Did you find this article valuable?

Support ADITYA DAS by becoming a sponsor. Any amount is appreciated!