Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:19:10+00:00 2026-05-31T08:19:10+00:00

I have a big string of formatted data (e.g. JSON) that I want to

  • 0

I have a big string of formatted data (e.g. JSON) that I want to dump to YAML using Psych in ruby while preserving formatting.

Basically, I want for JSON to appear in YAML using literal style:

---
json: |
  {
    "page": 1,
    "results": [
      "item", "another"
    ],
    "total_pages": 0
  }

However, when I use YAML.dump it doesn’t use literal style. I get something like this:

---
json: ! "{\n  \"page\": 1,\n  \"results\": [\n    \"item\", \"another\"\n  ],\n  \"total_pages\":
  0\n}\n"

How can I tell Psych to dump scalars in wanted style?


Solution:

Big thanks to Aaron Patterson for his solution that I’m expanding on here: https://gist.github.com/2023978

Although a bit verbose, that gist is a working way of tagging certain strings in ruby to be output using literal style in YAML.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:19:11+00:00Added an answer on May 31, 2026 at 8:19 am
    require 'psych'
    
    # Construct an AST
    visitor = Psych::Visitors::YAMLTree.new({})
    visitor << DATA.read
    ast = visitor.tree
    
    # Find all scalars and modify their formatting
    ast.grep(Psych::Nodes::Scalar).each do |node|
      node.plain  = false
      node.quoted = true
      node.style  = Psych::Nodes::Scalar::LITERAL
    end
    
    begin
      # Call the `yaml` method on the ast to convert to yaml
      puts ast.yaml
    rescue
      # The `yaml` method was introduced in later versions, so fall back to
      # constructing a visitor
      Psych::Visitors::Emitter.new($stdout).accept ast
    end
    
    __END__
    {
      "page": 1,
      "results": [
        "item", "another"
    ],
      "total_pages": 0
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big string having at most 100000 character. Instead of using string.charAt[index]
I have a JLabel that needs to display some html-formatted text. However, I want
So I have a big long query string that can either be ... //url=z&surl=y&time=z&codec=a264&acodec=mp3&width=400x100
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to
I have a Big string which has multiple mathmls in it. Want to take
I have a big string, and want to find the first occurrence of X,
I have a big database that has fields of paragraphs that are formatted like
I have a Big html in String variable and I want to get contents
I have a big php object that I want to serialize and store in
I have a big string (let's call it a CSV file, though it isn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.