How do you know that got: JSON or not?

There is a link to JSON. Information it parsed in RoR application.
Sometimes on a remote server instead of JSON'and you receive a 503 error saying the server is currently unavailable.

url = 'this url'
uri = URI(url)
response = Net::HTTP.get(uri)
data = JSON.parse(response)


How to implement the test? For example, how to test what is received from the server? I want to implement a check for "JSON'ness".

You can of course first check the status of the server response, Mr. may, is there any other option provided for in Ruby/RoR in conjunction with the JSON class?
July 9th 19 at 10:45
2 answers
July 9th 19 at 10:47
Solution
Yes, in General, just check on the status and intended to verify the information.

usually written, if the status is 200, then begin to parse JSON.
Understood, thank you :) - alfonzo.Zeml commented on July 9th 19 at 10:50
And the answer is 400 too right? Is there still any true codes ovatow? - alfonzo.Zeml commented on July 9th 19 at 10:53
: 400 indicates that the request is made is not true - if you receive a result, it is likely he doesn't meet your request, and data you are given is incorrect.

In General, all statuses are unambiguous. So you need to try to only get 200 status. All others are considered incorrect for data processing. - Maggie18 commented on July 9th 19 at 10:56
: understood. Just now only gives me code 400, not 200. The server returns JSON that is parsed successfully... - alfonzo.Zeml commented on July 9th 19 at 10:59
: That after you, someone else successfully picking code, it is better to bring the request to the state, which would have been a 200 status.

If you need to tackle on the knee, then of course it is possible and 400 to parse, if you are satisfied with the data. Well, it's nomination for bad smelling code ) - Maggie18 commented on July 9th 19 at 11:02
all was sorted out. Thank you. - alfonzo.Zeml commented on July 9th 19 at 11:05
July 9th 19 at 10:49
def valid_json?(json)
begin
JSON.parse(json)
 return true
 rescue JSON::ParserError => e
 return false
end
end
This option seems better as we get rid of one query to check the status. Right? - alfonzo.Zeml commented on July 9th 19 at 10:52
: XS is a simple function which checks a valid JSON or not, and generally the best course rukovodstvuetsya http response code. - alfonzo.Zeml commented on July 9th 19 at 10:55

Find more questions by tags JSONRubyRuby on Rails