I'm trying to parse a JSON string that I received from Twitter as a response via the RESTful API. I wanted to use the JSON gem, but I've run into an issue. Per the documentation, it appears that the JSON parser returns a Hash, which is what I want. However, when I actually perform the JSON.parse(twitter_result.body), it returns an Array. How can I get the JSON parser to return a Hash?
This is the parser I am using: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parse
"Pretty" JSON output example:
[
  {
    "created_at": "Sun Dec 08 13:00:52 +0000 2013",
    "id": 409668902189137920,
    "id_str": "409668902189137920",
    "text": "\"All the people departed, and David went home to bless his household.\" 1 Chron. 16:43. Big meeting. Blessed home. Both.",
    "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</a>",
    "truncated": false,
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
      "id": 27500565,
      "id_str": "27500565",
      "name": "John Piper",
      "screen_name": "JohnPiper",
      "location": "Minneapolis, MN",
      "description": "Founder-Teacher, Desiring God, Chancellor, BCS. Spreading a passion for the supremacy of God in all things for the joy of all peoples through Jesus Christ.",
      "url": "http://t.co/qt1rUlRfdl",
      "entities": {
        "url": {
          "urls": [
            {
              "url": "http://t.co/qt1rUlRfdl",
              "expanded_url": "http://www.desiringgod.org",
              "display_url": "desiringgod.org",
              "indices": [
                0,
                22
              ]
            }
          ]
        },
        "description": {
          "urls": [
          ]
        }
      },
      "protected": false,
      "followers_count": 564749,
      "friends_count": 107,
   [
  {
    "created_at": "Sun Dec 08 13:00:52 +0000 2013",
    "id": 409668902189137920,
    "id_str": "409668902189137920",
    "text": "\"All the people departed, and David went home to bless his household.\" 1 Chron. 16:43. Big meeting. Blessed home. Both.",
    "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</a>",
    "truncated": false,
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
      "id": 27500565,
      "id_str": "27500565",
      "name": "John Piper",
      "screen_name": "JohnPiper",
      "location": "Minneapolis, MN",
      "description": "Founder-Teacher, Desiring God, Chancellor, BCS. Spreading a passion for the supremacy of God in all things for the joy of all peoples through Jesus Christ.",
      "url": "http://t.co/qt1rUlRfdl",
      "entities": {
        "url": {
          "urls": [
            {
              "url": "http://t.co/qt1rUlRfdl",
              "expanded_url": "http://www.desiringgod.org",
              "display_url": "desiringgod.org",
              "indices": [
                0,
                22
              ]
            }
          ]
        },
        "description": {
          "urls": [
          ]
        }
      },
      "protected": false,
      "followers_count": 564749,
      "friends_count": 107,
      "listed_count": 9225,
      "created_at": "Sun Mar 29 20:48:44 +0000 2009",
      "favourites_count": 20,
      "utc_offset": -21600,
      "time_zone": "Central Time (US & Canada)",
      "geo_enabled": true,
      "verified": true,
      "statuses_count": 7906,
      "lang": "en",
      "contributors_enabled": false,
      "is_translator": false,
      "profile_background_color": "570000",
      "profile_background_image_url": "http://a0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
      "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
      "profile_background_tile": false,
      "profile_image_url": "http://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
      "profile_link_color": "5C0606",
      "profile_sidebar_border_color": "000000",
      "profile_sidebar_fill_color": "CCC2A3",
      "profile_text_color": "000000",
      "profile_use_background_image": true,
      "default_profile": false,
      "default_profile_image": false,
      "following": true,
      "follow_request_sent": false,
      "notifications": false
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 20,
    "favorite_count": 31,
    "entities": {
      "hashtags": [
      ],
      "symbols": [
      ],
      "urls": [
      ],
      "user_mentions": [
      ]
    },
    "favorited": false,
    "retweeted": false,
    "lang": "en"
  }
]   "listed_count": 9225,
      "created_at": "Sun Mar 29 20:48:44 +0000 2009",
      "favourites_count": 20,
      "utc_offset": -21600,
      "time_zone": "Central Time (US & Canada)",
      "geo_enabled": true,
      "verified": true,
      "statuses_count": 7906,
      "lang": "en",
      "contributors_enabled": false,
      "is_translator": false,
      "profile_background_color": "570000",
      "profile_background_image_url": "http://a0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
      "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
      "profile_background_tile": false,
      "profile_image_url": "http://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
      "profile_link_color": "5C0606",
      "profile_sidebar_border_color": "000000",
      "profile_sidebar_fill_color": "CCC2A3",
      "profile_text_color": "000000",
      "profile_use_background_image": true,
      "default_profile": false,
      "default_profile_image": false,
      "following": true,
      "follow_request_sent": false,
      "notifications": false
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweet_count": 20,
    "favorite_count": 31,
    "entities": {
      "hashtags": [
      ],
      "symbols": [
      ],
      "urls": [
      ],
      "user_mentions": [
      ]
    },
    "favorited": false,
    "retweeted": false,
    "lang": "en"
  }
]
                body), it returns an Array.
JSON isn't code, you can't inject harmful values into it. JSON. parse is safe.
JavaScript Object Notation (JSON)¶ ↑ JSON is a lightweight data-interchange format. A JSON value is one of the following: Double-quoted text: "foo" . Number: 1 , 1.0 , 2.0e2 .
JSON output returns an array with one element which is the Hash you're looking for.
Try this:
puts JSON.parse(twitter_result.body)[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With