json_to_yaml

package
v0.95.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "json-to-yaml <json-file>",
	Short: "Convert JSON file to YAML in place",
	Long: `Convert a JSON file to YAML format.
Creates a new .yaml file and removes the original .json file.

Example: slu scripts json-to-yaml example.json
  Creates: example.yaml
  Removes: example.json`,
	Args: cobra.ExactArgs(1),
	Run: func(c *cobra.Command, args []string) {
		jsonFilePath := args[0]

		jsonData, err := os.ReadFile(jsonFilePath)
		error_utils.HandleError(err, "Failed to read JSON file:")

		// Unmarshal JSON to generic interface
		var data interface{}
		err = json.Unmarshal(jsonData, &data)
		error_utils.HandleError(err, "Failed to parse JSON file:")

		yamlData, err := yaml.Marshal(data)
		error_utils.HandleError(err, "Failed to convert to YAML:")

		ext := filepath.Ext(jsonFilePath)
		var yamlFilePath string
		if ext == ".json" {
			yamlFilePath = strings.TrimSuffix(jsonFilePath, ext) + ".yaml"
		} else {

			yamlFilePath = jsonFilePath + ".yaml"
		}

		err = os.WriteFile(yamlFilePath, yamlData, 0644)
		error_utils.HandleError(err, "Failed to write YAML file:")

		err = os.Remove(jsonFilePath)
		error_utils.HandleError(err, "Failed to remove JSON file:")
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL