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 8462937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:19:11+00:00 2026-06-10T14:19:11+00:00

I’m new to Ruby and had a question. I’m trying to create a .rb

  • 0

I’m new to Ruby and had a question. I’m trying to create a .rb file that converts JSON to CSV.

I came across some disparate sources that got me to make:

require "rubygems"
require 'fastercsv'
require 'json'

csv_string = FasterCSV.generate({}) do |csv|
   JSON.parse(File.open("small.json").read).each do |hash|
    csv << hash
  end
end

puts csv_string

Now, it does in fact output text but they are all squashed together without spaces, commas etc. How do I make it more customised, clear for a CSV file so I can export that file?

The JSON would look like:

        {
            "results": [
                {
                    "reportingId": "s", 
                    "listingType": "Business", 
                    "hasExposureProducts": false, 
                    "name": "Medeco Medical Centre World Square", 
                    "primaryAddress": {
                        "geoCodeGranularity": "PROPERTY", 
                        "addressLine": "Shop 9.01 World Sq Shopng Cntr 644 George St", 
                        "longitude": "151.206172", 
                        "suburb": "Sydney", 
                        "state": "NSW", 
                        "postcode": "2000", 
                        "latitude": "-33.876416", 
                        "type": "VANITY"
                    }, 

                    "primaryContacts": [
                        {
                            "type": "PHONE", 
                            "value": "(02) 9264 8500"
                        }
                    ]
                },xxx
        }

The CSV to just have something like:

        reportingId, s, listingType, Business, name, Medeco Medical...., addressLine, xxxxx, longitude, xxxx, latitude, xxxx, state, NSW, postcode, 2000, type, phone, value, (02) 92648544             
  • 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-06-10T14:19:13+00:00Added an answer on June 10, 2026 at 2:19 pm

    Since your JSON structure is a mix of hashes and lists, and also has levels of different heights, it is not as trivial as the code you show. However (assuming your input files always look the same) it shouldn’t be hard to write an appropriate converter. On the lowest level, you can transform a hash to CSV by

    hash.to_a.flatten
    

    E.g.

    input = JSON.parse(File.open("small_file.json").read)
    writer = FasterCSV.open("out.csv", "w")
    writer << input["results"][0]["primaryAddress"].to_a.flatten
    

    will give you

    type,VANITY,latitude,-33.876416,postcode,2000,state,NSW,suburb,Sydney,longitude,151.206172,addressLine,Shop 9.01 World Sq Shopng Cntr 644 George St,geoCodeGranularity,PROPERTY
    

    Hope that guides you the direction.

    Btw, your JSON looks invalid. You should change the },xxx line to }].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.