{"id":891,"date":"2021-07-26T15:55:20","date_gmt":"2021-07-26T15:55:20","guid":{"rendered":"https:\/\/techno.slomka.biz\/?p=891"},"modified":"2026-03-16T15:16:31","modified_gmt":"2026-03-16T15:16:31","slug":"convert-json-to-yaml-and-back-using-python","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=891","title":{"rendered":"Convert JSON to YAML and back using python"},"content":{"rendered":"\n<p>Just a few lines of code to convert json to yaml and back.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-3e98d35f-68ac-47bf-8592-5f62d96b76e0\">Convert JSON to YAML <\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">Python<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import json\nimport yaml\n\nin_file = sys.argv&#91;1&#93;\nout_file = sys.argv&#91;2&#93;\n\nwith open(in_file, 'r') as json_in, open(out_file, \"w\") as yaml_out:\n    json_payload = json.load(json_in)\n    yaml.dump(json_payload,sort_keys=False)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">import<\/span><span style=\"color: #EEFFFF\"> json<\/span><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">import<\/span><span style=\"color: #EEFFFF\"> yaml<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">in_file <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> sys<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">argv<\/span><span style=\"color: #89DDFF\">&#91;<\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #89DDFF\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">out_file <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> sys<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">argv<\/span><span style=\"color: #89DDFF\">&#91;<\/span><span style=\"color: #F78C6C\">2<\/span><span style=\"color: #89DDFF\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">with<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">open<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">in_file<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #C3E88D\">r<\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #89DDFF\">)<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF; font-style: italic\">as<\/span><span style=\"color: #EEFFFF\"> json_in<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">open<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">out_file<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">w<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF; font-style: italic\">as<\/span><span style=\"color: #EEFFFF\"> yaml_out<\/span><span style=\"color: #89DDFF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    json_payload <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> json<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">load<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">json_in<\/span><span style=\"color: #89DDFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    yaml<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">dump<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">json_payload<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF; font-style: italic\">sort_keys<\/span><span style=\"color: #89DDFF\">=False)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-3e98d35f-68ac-47bf-8592-5f62d96b76e0\">Convert YAML to JSON<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">Python<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import yaml\nimport json\n\nin_file = sys.argv&#91;1&#93;\nout_file = sys.argv&#91;2&#93;\n\nwith open(in_file, 'r') as yaml_in, open(out_file, \"w\") as json_out:\n    yaml_playload = yaml.safe_load(yaml_in) \n    json.dump(yaml_object, json_out)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">import<\/span><span style=\"color: #EEFFFF\"> yaml<\/span><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">import<\/span><span style=\"color: #EEFFFF\"> json<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">in_file <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> sys<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">argv<\/span><span style=\"color: #89DDFF\">&#91;<\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #89DDFF\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">out_file <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> sys<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #F07178\">argv<\/span><span style=\"color: #89DDFF\">&#91;<\/span><span style=\"color: #F78C6C\">2<\/span><span style=\"color: #89DDFF\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF; font-style: italic\">with<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">open<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">in_file<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #C3E88D\">r<\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #89DDFF\">)<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF; font-style: italic\">as<\/span><span style=\"color: #EEFFFF\"> yaml_in<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">open<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">out_file<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">w<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #89DDFF\">)<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF; font-style: italic\">as<\/span><span style=\"color: #EEFFFF\"> json_out<\/span><span style=\"color: #89DDFF\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    yaml_playload <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> yaml<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">safe_load<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">yaml_in<\/span><span style=\"color: #89DDFF\">)<\/span><span style=\"color: #EEFFFF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    json<\/span><span style=\"color: #89DDFF\">.<\/span><span style=\"color: #82AAFF\">dump<\/span><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">yaml_object<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #82AAFF\"> json_out<\/span><span style=\"color: #89DDFF\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a few lines of code to convert json to yaml and back. Convert JSON to YAML Convert YAML to JSON<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[125,116,119],"class_list":["post-891","post","type-post","status-publish","format-standard","hentry","category-python","tag-json","tag-python","tag-yaml"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=891"}],"version-history":[{"count":11,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/891\/revisions"}],"predecessor-version":[{"id":1114,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/891\/revisions\/1114"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}