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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:44:02+00:00 2026-06-11T04:44:02+00:00

I am trying to parse through a JSON response for customer data (names and

  • 0

I am trying to parse through a JSON response for customer data (names and email) and construct a csv file with column headings of the same.

For some reason, every time I run this code I get a CSV file with a list of all the first names in one cell (with no separation in between the names…just a string of names appended to each other) and the same thing for the last name. The following code does not include adding emails (I’ll worry about that later).

Code:

 def self.fetch_emails

    access_token ||= AssistlyArticle.remote_setup
    cust_response = access_token.get("https://blah.desk.com/api/v1/customers.json")
    cust_ids = JSON.parse(cust_response.body)["results"].map{|w| w["customer"]["id"].to_i}

 FasterCSV.open("/Users/default/file.csv", "wb") do |csv|
      # header row
      csv << ["First name", "Last Name"]
      # data rows
      cust_ids.each do |cust_firstname|
        json = JSON.parse(cust_response.body)["results"]
        csv << [json.map{|x| x["customer"]["first_name"]}, json.map{|x| x["customer"]["last_name"]}]
      end 
    end
  end

Output:

First Name        | Last Name
JohnJillJamesBill   SearsStevensSethBing

and so on…

Desired Output:

First Name | Last Name
John       | Sears
Jill       | Stevens
James      | Seth
Bill       | Bing

Sample JSON:

{
    "page":1,
    "count":20,
    "total":541,
    "results":
    [
        {
            "customer":
            {
                "custom_test":null,
                "addresses":
                [
                    {
                        "address":
                        {
                            "region":"NY",
                            "city":"Commack",
                            "location":"67 Harned Road,
                             Commack,
                             NY 11725,
                             USA",
                            "created_at":"2009-12-22T16:21:23-05:00",
                            "street_2":null,
                            "country":"US",
                            "updated_at":"2009-12-22T16:32:37-05:00",
                            "postalcode":"11725",
                            "street":"67 Harned Road",
                            "lng":"-73.196225",
                            "customer_contact_type":"home",
                            "lat":"40.716894"
                        }
                    }
                ],
                "phones":
                [
                ],
                "last_name":"Suriel",
                "custom_order":"4",
                "first_name":"Jeremy",
                "custom_t2":"",
                "custom_i":"",
                "custom_t3":null,
                "custom_t":"",
                "emails":
                [
                    {
                        "email":
                        {
                            "verified_at":"2009-11-27T21:41:11-05:00",
                            "created_at":"2009-11-27T21:40:55-05:00",
                            "updated_at":"2009-11-27T21:41:11-05:00",
                            "customer_contact_type":"home",
                            "email":"jeremysuriel+twitter@gmail.com"
                        }
                    }
                ],
                "id":8,
                "twitters":
                [
                    {
                        "twitter":
                        {
                            "profile_image_url":"http://a3.twimg.com...",
                            "created_at":"2009-11-25T10:35:56-05:00",
                            "updated_at":"2010-05-29T22:41:55-04:00",
                            "twitter_user_id":12267802,
                            "followers_count":93,
                            "verified":false,
                            "login":"jrmey"
                        }
                    }
                ]
            }
        },
        {
            "customer":
            {
                "custom_test":null,
                "addresses":
                [
                ],
                "phones":
                [
                ],
                "last_name":"",
                "custom_order":null,
                "first_name":"jeremy@example.com",
                "custom_t2":null,
                "custom_i":null,
                "custom_t3":null,
                "custom_t":null,
                "emails":
                [
                    {
                        "email":
                        {
                            "verified_at":null,
                            "created_at":"2009-12-05T20:39:00-05:00",
                            "updated_at":"2009-12-05T20:39:00-05:00",
                            "customer_contact_type":"home",
                            "email":"jeremy@example.com"
                        }
                    }
                ],
                "id":27,
                "twitters":
                [
                    null
                ]
            }
        }
    ]
}

Is there a better use of FasterCSV to allow this? I assumed that << would add to a new row each time…but it doesn’t seem to be working. I would appreciate any help!

  • 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-11T04:44:03+00:00Added an answer on June 11, 2026 at 4:44 am

    You’ve got it all tangled up somehow, you’re parsing the json too many times (and inside a loop!) Let’s make it simpler:

    customers = JSON.parse(data)["results"].map{|x| x['customer']}
    customers.each do |c|
      csv << [c['first_name'], c['last_name']]
    end
    

    also ‘wb’ is the wrong mode for csv – just ‘w’.

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

Sidebar

Related Questions

I'm trying handle bad json data when parsed through json_decode(). I'm using the following
I'm trying to parse CSV files uploaded by the user through PHP, but it's
I'm trying to fetch and parse an XML-file through JavaScript. I don't control the
I'm trying to parse JSON in JavaScript. If my JSON data looks like below,
I'm trying to use the JSONKIt found here https://github.com/johnezang/JSONKit to parse through a JSON
Hi I'm a complete newbie to json. I'm trying to parse a json file
I am trying to parse JSON response from a httpwebrequest fetch and a little
I'm trying to access some data through JSON RPC using the trac XMLRPCPlugin using
I am trying to parse through a JSON string and convert it to the
I'm downloading a file and trying to use JSON.parse, which should return { dateTime:

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.