1

I am running Python script to get data as dict (json) how can I convent this Json dict to yaml without reading / writing to a file.

just to convent Json object to YAML object. thanks

Sahard
  • 51
  • 6
  • Does this answer your question? [dump json into yaml](https://stackoverflow.com/questions/15941996/dump-json-into-yaml) – sushanth May 06 '21 at 12:16
  • There is no such thing as a *JSON object* or a *YAML object*. Both are serialization formats and, when loaded, are converted to native types (e.g. in Python `dict`, `list` etc) which lose the information that they were encoded in JSON or YAML. You likely have an *input* (stream or string) that contains data serialized as JSON, and want to convert this into in output stream or string that has the same data serialized as YAML. Since YAML is a superset of JSON, you do not need to do anything, your JSON stream will load as valid YAML as-is. – flyx May 06 '21 at 13:07
  • thanks for this info – Sahard May 06 '21 at 16:13

1 Answers1

0
import simplejson
import sys
import yaml
 
print yaml.dump(simplejson.loads(str(sys.stdin.read())), default_flow_style=False)

Python one-liner: converting JSON to YAML Check this: https://blog.jasoncallaway.com/2015/10/11/python-one-liner-converting-json-to-yaml/

modo24
  • 64
  • 1
  • 9