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

  • SEARCH
  • Home
  • 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 9229525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:35:36+00:00 2026-06-18T05:35:36+00:00

I am trying to write a rake task to parse a file containing lots

  • 0

I am trying to write a rake task to parse a file containing lots of twitter updates in json format.

The json file looks like this:

{ "_id" : { "$oid" : "50f82aeab4aa879861000000" }, "text" : "Golden Globes, lots of fashion messes...but glad Argo won. Really good movie, along w Moonrise Kingdom and Salmon Fishing Yeman.", "created_at" : { "$date" : 1358137508000 }, "id" : 290675893024747523, "user" : { "screen_name" : "Dpharmakis23", "id" : 852045842 } }
{ "_id" : { "$oid" : "50f82aeab4aa879861000001" }, "text" : "RT @RPLife: 2 Red Carpet HQs added and maybe my favorites of the night http://t.co/Fiq08m7i", "created_at" : { "$date" : 1358137507000 }, "id" : 290675892668211203, "user" : { "screen_name" : "dri_violet", "id" : 88925839 } }
{ "_id" : { "$oid" : "50f82aeab4aa879861000002" }, "text" : "After watching the Golden Globes it made me want to watch Les Mis again. It's gonna be a late night tonight.", "created_at" : { "$date" : 1358137507000 }, "id" : 290675892416573440, "user" : { "screen_name" : "JessBess4", "id" : 273137370 } }
{ "_id" : { "$oid" : "50f82aeab4aa879861000003" }, "text" : "I'm pretty how I've felt during the Golden Globes is how most of my followers feel during a NASCAR race/football game. #TweetOverload", "created_at" : { "$date" : 1358137507000 }, "id" : 290675892311715840, "user" : { "screen_name" : "JordanTMcGraw", "id" : 25172777 } }
{ "_id" : { "$oid" : "50f82aeab4aa879861000004" }, "text" : "RT @LaAbuela961: Hay personas que solo compran ropa en Jacarandas pero se pasaron criticando los vestuarios de los famosos en los Golden Globes. #NoMeJodan", "created_at" : { "$date" : 1358137507000 }, "id" : 290675892278157313, "user" : { "screen_name" : "silvita_hdez", "id" : 269487031 } }

What I want to do is parse each of those entries into my “tweets” model, which in the schema looks like this:

create_table "tweets", :force => true do |t|
  t.integer  "tid"
  t.datetime "created_at", :null => false
  t.text     "text"
  t.string   "user_name"
  t.integer  "user_id"
  t.datetime "updated_at", :null => false
end

The data from the twitter JSON should go into my table as follows:

  • _id.user.screen_name -> Tweet.user_name
  • _id.user.id -> Tweet.user_id
  • _id.text -> Tweet.text
  • _id.created_at -> Tweet.created_at
  • _id.id -> Tweet.tid

The rake task I have written so far looks like this:

require 'multi_json'

namespace :db do
  task :import_tweets => :environment do
    File.open('/omittedPath/5tweets.json', 'r') do |file|
      file.each do |line|
        attrs = JSON.parse line
        twt = Tweet.find_or_initialize_by_identifier(attrs[2])
        twt.save!
      end
    end
  end
end

When I tried to parse the twitter json in the rails console, I ran the command:

JSON.parse { "_id" : { "$oid" : "50f840c2b4aa879ae4000d5d" }, "text" : "Nanananana BATMAN! Christian Bale is presenting now. #GoldenGlobes", "created_at" : { "$date" : 1358134191000 }, "id" : 290661983240454144, "user" : { "screen_name" : "egbrown27", "id" : 377696330 } }

but the response I got was

SyntaxError: (irb):11: syntax error, unexpected ':', expecting '}'
JSON.parse { "_id" : { "$oid" : "50f840c2b4aa879a...
                ^
(irb):11: syntax error, unexpected ':', expecting tASSOC
...JSON.parse { "_id" : { "$oid" : "50f840c2b4aa879ae4000d5d" }...
...                               ^
(irb):11: syntax error, unexpected ',', expecting $end
...: "50f840c2b4aa879ae4000d5d" }, "text" : "Nanananana BATMAN!...

Any help would be greatly appreciated. Thank you!

  • 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-18T05:35:38+00:00Added an answer on June 18, 2026 at 5:35 am

    You should pass a string to JSON.parse. Try this:

    JSON.parse '{ "_id" : { "$oid" : "50f840c2b4aa879ae4000d5d" }, "text" : "Nanananana BATMAN! Christian Bale is presenting now. #GoldenGlobes", "created_at" : { "$date" : 1358134191000 }, "id" : 290661983240454144, "user" : { "screen_name" : "egbrown27", "id" : 377696330 } }'
    

    It will return a parsed hash:

    {"_id"=>{"$oid"=>"50f840c2b4aa879ae4000d5d"}, "text"=>"Nanananana BATMAN! Christian Bale is presenting now. #GoldenGlobes", "created_at"=>{"$date"=>1358134191000}, "id"=>290661983240454144, "user"=>{"screen_name"=>"egbrown27", "id"=>377696330}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a 3rd party rake task that looks like this: abort(rake
I'm trying to write a rake task for loading data into my database. My
I'm trying to write a rake task that will set up an environment mirroring
I'm trying to write a custom Rake task to perform some tests for a
I'm trying to write a Rails 3 rake task to generate config/initializers/secret_token.rb . I
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a chat, like on facebook, I wondered if two clients can
I'm working on a rake task which imports from a JSON feed into an
I'm trying to run an example task that I wrote. I've placed the file

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.