How to convert JSON to YAML in javascript

后端 未结 4 1067
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 11:23

I want to convert a json string to yaml format in javascript.I am trying my hand on google from last two days didnt found any solution or libraries. There are answers availa

4条回答
  •  不知归路
    2020-12-06 11:45

    I tried to package the answer to a bash script.

    #!/bin/bash
    #convert-json-to-yaml.sh
    
    if [[ "$1" == "" ]]; then
        echo "You must provide a json file in argument i.e. ./convert-json-to-yaml.sh your_file.json"
        exit 1
    fi
    
    jsonFile=$1
    yamlFile="$1.yaml"
    
    if [[ "$2" != "" ]]; then
        yamlFile=$2
    fi
    
    python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < ${jsonFile} > ${yamlFile}
    

提交回复
热议问题