question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to dump blocks without having quotes on keys

See original GitHub issue

I am trying to dump JSON data into YAML in a way that makes is very easy to read by humans (as this goes to some logs).

Some of the values contain multiline strigs (stderr/stdout) and there is a must to avoid wrapping them.

    data = json.loads("""{"key":"foo\\nbar"}""")
    output = yaml.safe_dump(data,
        allow_unicode=True,
        default_flow_style=False,
        canonical=False,
        default_style="|")
    print(output)

    output = yaml.safe_dump(data,
        allow_unicode=True,
        default_flow_style=False,
        canonical=False,
        default_style=None)
    print(output)

First formatting:

"key": |-
  foobar

Second option:

key: 'foo

  bar'

It seem that with default_style="|" I do get the desired wrapping of multiline values but I lose the simplicity of the unquoted lines.

How can I get something like below?

key: |
  foo
  bar

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:7

github_iconTop GitHub Comments

1reaction
ysaakprcommented, Aug 30, 2019

looks like the if the value has ":" in it, then the whole value has to be in quotes in order to work with quotes

0reactions
Yop-Lacommented, Feb 28, 2022

Use ruamel.yaml instead

it is better documented than pyyaml: https://pypi.org/project/ruamel.yaml/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ruamel Yaml dump in Python: Need to ensure specific keys ...
Issue With Current Solution: The keys are ever changing and new keys are constantly being added, but the data structure will always remain...
Read more >
thesis know-how – beware the quote dump | patter
A quote dump is when the writer inserts a very large extract of someone ... And one or two quotations without any commentary...
Read more >
Strings in YAML - To Quote or not to Quote | tinita [blogs.perl.org]
The good news is, the YAML double quoted string works the same as in JSON, so if you know that already, you will...
Read more >
json.dumps() in Python - GeeksforGeeks
JSON utilizes comma delimited key value pairs contained in double quotes and separated by colons. The body of a JSON file can be...
Read more >
Online Quotes - GnuCash
The Finance::Quote software gathers its price data from several Web sites ... gnc-fq-dump yahoo_json M&M.NS [1] 1732 If 'M.NS' is not a typo...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found