Gregory Worrall
Gregory Worrall

Reputation: 181

How do I extract from this JSON?

I'm trying to extract data from this JSON, however when I type [0], it returns '{' & if I type ["tweets0"], I get nothing. Am I missing something really obvious?

I'm using Ruby & the MultiJson gem, if that changes anything.

{
    "tweets0": [
        {
            "content": "Test1",
            "time": "2015/08/16 7:43 PM"
        }
    ],
    "tweets1": [
        {
            "content": "Test2",
            "time": "2015/08/16 7:44 PM"
        }
    ],
    "tweets2": [
        {
            "content": "Test3",
            "time": "2015/08/16 7:44 PM"
        }
    ],
    "tweets3": [
        {
            "content": "Test3",
            "time": "2015/08/16 7:46 PM"
        }
    ],
    "tweets4": [
        {
            "content": "Test",
            "time": "2015/08/16 7:45 PM"
        }
    ],
    "tweets5": [
        {
            "content": "3",
            "time": "2015/08/16 7:48 PM"
        }
    ],
    "tweets6": [
        {
            "content": "3",
            "time": "2015/08/16 7:48 PM"
        }
    ],
    "tweets7": [
        {
            "content": "3213",
            "time": "2015/08/16 7:49 PM"
        }
    ],
    "tweets8": [
        {
            "content": "3213",
            "time": "2015/08/16 7:49 PM"
        }
    ],
    "tweets9": [
        {
            "content": "23",
            "time": "2015/08/16 7:50 PM"
        }
    ],
    "tweets10": [
        {
            "content": "23",
            "time": "2015/08/16 7:49 PM"
        }
    ],
    "tweets11": [
        {
            "content": "3",
            "time": "2015/08/16 7:53 PM"
        }
    ],
    "tweets12": [
        {
            "content": "34",
            "time": "2015/08/16 7:53 PM"
        }
    ],
    "tweets13": [
        {
            "content": "25",
            "time": "2015/08/16 7:53 PM"
        }
    ],
    "tweets14": [
        {
            "content": "right",
            "time": "2015/08/16 7:52 PM"
        }
    ]
}

Upvotes: 0

Views: 55

Answers (3)

Matt
Matt

Reputation: 74801

Once you have loaded your data with MultiJson

> require 'multi_json'
> json = MultiJson.load('{"tweets0": [{"content": "Test1","time": "2015/08/16 7:43 PM"}]}')
=> {"tweets0"=>[{"content"=>"Test1", "time"=>"2015/08/16 7:43 PM"}]}

Then you can access a Hash element via name:

> json['tweets0']
=>[{"content"=>"Test1", "time"=>"2015/08/16 7:43 PM"}]

To access elements of the array:

> json['tweets0'].first
=> {"content"=>"Test1", "time"=>"2015/08/16 7:43 PM"}

Upvotes: 0

Piccolo
Piccolo

Reputation: 1666

Why not just use the standard json library built into Ruby?

require 'json'
a = JSON.parse(your_json_data)
a["tweets0"] #=> [{"content"=>"Test1", "time"=>"2015/08/16 7:43 PM"}]

Upvotes: 0

mrcheshire
mrcheshire

Reputation: 535

If [0] returns '{', than that suggests to me that Ruby isn't treating your JSON as a JSON object, but as a string.

Maybe you need to .load() your JSON first?

Upvotes: 2

Related Questions