Saturday, March 2, 2024
HomeJavaConvert JSON file to YAML utilizing JQ - Java Code Geeks

Convert JSON file to YAML utilizing JQ – Java Code Geeks

What’s this text about?

If you find yourself working on the net or with DevOps or GitOps, you should have circumstances the place you wish to remodel a JSON file to YAML. This text walks you although changing the JSON doc to YAML utilizing the “jq” software.

Why YAML (over JSON)?

Listed below are a couple of the reason why builders choose YAML over JSON:

  • YAML is visually simpler to take a look at. (Typically the {…} syntax with JSON can overwhelm your eyes with an excessive amount of noise.)
  • YAML has help for feedback (JSON doesn’t have a manner so as to add feedback.) YAML feedback start with the quantity signal (#)
  • YAML has helps multiline strings utilizing the “block fashion indicator” (|). It additional enhances it with “block chomping indicator” with using (+), (-), default.
    • The block fashion signifies how newlines contained in the block ought to behave.
    • The chomping indicator controls what ought to occur with newlines on the finish of the string.
    • See YAML Multiline for an excellent description on the utilization.


  • Be sure you have downloaded JQ and positioned it in your PATH.
  • Edit and place the next into ~/.jq
def yamlify2:
    (objects | to_entries | (map(.key | size) | max + 2) as $w |
        .[] | (.worth | sort) as $sort |
        if $sort == "array" then
            "(.key):", (.worth | yamlify2)
        elif $sort == "object" then
            "(.key):", "    (.worth | yamlify2)"
            "(.key):(" " * (.key | $w - size))(.worth)"
    // (arrays | choose(size > 0)[] | [yamlify2] |
        "  - (.[0])", "    (.[1:][])"
    // .

The above code provides a brand new perform “yamlify2” to your library of jq capabilities.

JSON to YAML conversion utilizing JQ

Now we’re able to convert a JSON doc to YAML.


jq -r yamlify2 enter.json
# or
jq -r yamlify2 enter.json > output.yaml

Working Instance:

Filename: glossary.json

    "glossary": {
        "title": "instance glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Commonplace Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages similar to DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
					"GlossSee": "markup"
jq -r yamlify2 glossary.json
# or
jq -r yamlify2 glossary.json > glossary.yaml

Outcome (underneath glossary.yaml)

    title:     instance glossary
        title:      S
                ID:         SGML
                SortAs:     SGML
                GlossTerm:  Commonplace Generalized Markup Language
                Acronym:    SGML
                Abbrev:     ISO 8879:1986
                    para:          A meta-markup language, used to create markup languages similar to DocBook.
                      - GML
                      - XML
                GlossSee:   markup

You must have a YAML file prepared to make use of in your mission.




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments