0

I'm really new to working with JSON in Ruby, and am having a hard time figuring out why a script I'm running is generating blank lines as a result. Let's say that I'm parsing a file where one element is not always present. I want to check if that element exists, and depending on the result name variables in a specific way. Here is what I've been trying

require 'rubygems'
require 'json'

file = File.open("/path/to/file.json", encoding: 'UTF-8')
json = file.read
data = JSON.parse(json)

if data["snapshots"][-1]["responses"][2].nil?
  item = something
elseif
  item = something else
end

puts item

This is not generating an error, but is just producing a blank line. I'm sure I'm doing something obvious wrong, but would appreciate any help. Thanks!

4
  • What does the actual data look like? Commented May 8, 2014 at 13:45
  • Sorry - it is long and had a lot of personally indentifiable information in it so it took me some time to clean it up. But it basically looks like this: d.pr/bj6U Commented May 8, 2014 at 13:57
  • Obviously there is a top level item called "snapshots" which I forgot to include. This one is correct: d.pr/f0mj Commented May 8, 2014 at 14:05
  • So it was simply a typo in 'elsif'. Commented May 8, 2014 at 14:48

1 Answer 1

1

Your main problem is that you had elseif instead of elsif

Full code using the JSON you provided below:

require 'json'

json = <<EOS
{
 "snapshots": [
    {
        "steps": 10,
        "responses": [
            {
                "tokens": [
                    "Answer"
                ],
                "questionPrompt": "Question1?"
            },
            {
                "tokens": [
                    "Answer"
                ],
                "questionPrompt": "Question2?"
            },
            {
                "locationResponse": {
                    "location": {
                        "speed": 0,
                        "timestamp": "2014-04-20T17: 28: 37-0400",
                        "longitude": "-xx.xxxxxxx",
                        "latitude": "xx.xxxxxx",
                        "verticalAccuracy": 3,
                        "course": 0,
                        "horizontalAccuracy": 5
                    },
                    "text": "Response"
                },
                "questionPrompt": "Question3?"
            },
            {
                "tokens": [
                    "Answer"
                ],
                "questionPrompt": "Question4?"
            }
        ],
        "battery": 0.75,
        "sectionIdentifier": "1-2014-5-7",
        "audio": {
            "avg": -49.84988,
            "peak": -39.73056
        },
        "background": 0,
        "date": "2014-05-07T23: 20: 57-0400",
        "location": {
            "speed": -1,
            "placemark": {
                "subAdministrativeArea": "County",
                "subLocality": "CityName",
                "thoroughfare": "Street",
                "administrativeArea": "xx",
                "subThoroughfare": "xxx",
                "postalCode": "xxxxx",
                "region": "<+xx.xxxxxx",
                "radius": 28.13,
                "country": "UnitedStates",
                "locality": "CityName",
                "name": "Address"
            },
            "timestamp": "2014-05-07T23: 20: 58-0400",
            "longitude": "-xx.xxxxxxx",
            "latitude": "xx.xxxxxxx",
            "verticalAccuracy": 10,
            "course": 0,
            "horizontalAccuracy": 65
        },
        "dwellStatus": 0,
        "weather": {
            "relativeHumidity": "68%",
            "visibilityKM": 16.1,
            "tempC": 13.3,
            "precipTodayIn": 0,
            "windKPH": 0,
            "latitude": 40.813984,
            "windDegrees": 159,
            "stationID": "xxxxxxxx",
            "visibilityMi": 10,
            "pressureIn": 30.2,
            "pressureMb": 1023,
            "feelslikeF": 55.9,
            "windGustKPH": 12.4,
            "longitude": -77.895775,
            "feelslikeC": 13.3,
            "precipTodayMetric": 0,
            "tempF": 55.9,
            "windDirection": "SSE",
            "dewpointC": 8,
            "uv": 0,
            "weather": "Overcast",
            "windGustMPH": 7.7,
            "windMPH": 0
        },
        "connection": 1,
        "sync": 0,
        "reportImpetus": 0,
        "draft": 0
    }
   ]
}
EOS

data = JSON.parse(json)

if data["snapshots"][-1]["responses"][2].nil?
 item = "was nil"
elsif # THIS WAS elseif before
 item = "NOT nil"
end

puts item
Sign up to request clarification or add additional context in comments.

5 Comments

Duh. Thanks! But one more thing: after the elsif statement it is telling me that line should have two "=", so item == "NOT nil"
What is telling you that? Can you post your exact code? I don't get that error when running this locally...
the Terminal in Mac yielded this: warning: found = in conditional, should be ==
And really, this solved my problem and I'm marking it correct. Because the code is long and, again, includes identifiable information, I'm not sure it's worth getting too specific.
Actually, it does not need that. You were correct the first time. Sorry for any confusion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.