jsoncmd

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSONFormatCmd = &gcli.Command{
	Name:    "format",
	Aliases: []string{"fmt", "pretty"},
	Desc:    "pretty format input JSON(5) contents",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&jfOpts.json5, "json5, 5", "mark input contents is json5 format")

		c.AddArg("json", "input JSON(5) contents for format, allow: @c, @in")
	},
	Func: func(c *gcli.Command, _ []string) error {
		src, err := apputil.ReadSource(c.Arg("json").String())
		if err != nil {
			return err
		}

		return outputFmtJSON(src)
	},
}

JSONFormatCmd instance

View Source
var JSONQueryCmd = &gcli.Command{
	Name:    "view",
	Aliases: []string{"get", "cat", "query"},
	Desc:    "format and query value from JSON(5) contents",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&jvOpts.json5, "json5, 5", "mark input contents is json5 format")
		c.StrOpt2(&jvOpts.query, "query, path, q, p", "The path for query sub value")
		c.BoolOpt2(&jvOpts.compressed, "compressed, c", "compressed output, not format")

		c.AddArg("json", "input JSON contents for format")
		c.AddArg("path", "The path for query sub value, same of --path")
	},
	Func: func(c *gcli.Command, _ []string) error {
		src, err := apputil.ReadSource(c.Arg("json").String())
		if err != nil {
			return err
		}

		if !c.Arg("path").IsEmpty() {
			jvOpts.query = c.Arg("path").String()
		}

		if jvOpts.query == "" {
			return outputFmtJSON(src)
		}

		var mp maputil.Data
		if !jvOpts.json5 {

			if err = json.Unmarshal([]byte(src), &mp); err != nil {
				return err
			}
		} else {
			if err = json5.Unmarshal([]byte(src), &mp); err != nil {
				return err
			}
		}

		value := mp.Get(jvOpts.query)
		s, err := strutil.ToStringWith(value)
		if err == nil {
			stdio.Writeln(s)
			return nil
		}

		bs, err1 := json.Marshal(value)
		if err1 != nil {
			return err1
		}

		if jvOpts.compressed {
			stdio.WritelnBytes(bs)
			return nil
		}

		// format output
		var buf bytes.Buffer
		err = json.Indent(&buf, bs, "", "    ")
		if err != nil {
			return err
		}

		stdio.WriteBytes(buf.Bytes())
		return nil
	},
}

JSONQueryCmd instance

View Source
var JSONToYAMLCmd = &gcli.Command{
	Name:    "yaml",
	Aliases: []string{"to-yaml", "to-yml"},
	Desc:    "convert create table SQL to markdown table",
	Config: func(c *gcli.Command) {
		c.AddArg("json", "input JSON contents for convert")
	},
	Func: func(c *gcli.Command, _ []string) error {
		src, err := apputil.ReadSource(c.Arg("json").String())
		if err != nil {
			return err
		}

		bs, err := yaml.JSONToYAML([]byte(src))
		if err != nil {
			return err
		}

		fmt.Println(string(bs))
		return nil
	},
}

JSONToYAMLCmd instance

View Source
var JSONToolCmd = &gcli.Command{
	Name: "json",
	Desc: "json format, convert tool commands",
	Subs: []*gcli.Command{
		JSONQueryCmd,
		JSONToYAMLCmd,
		JSONFormatCmd,
	},
}

JSONToolCmd instance

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