Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a string is valid json before trying to parse it?

Tags:

json

ruby

In Ruby, is there a way to check if a string is valid json before trying to parse it?

For example getting some information from some other urls, sometimes it returns json, sometimes it could return a garbage which is not a valid response.

My code:

def get_parsed_response(response)   parsed_response = JSON.parse(response) end 
like image 431
Sam Avatar asked Oct 07 '14 09:10

Sam


People also ask

How do you check if a string is a valid JSON string in Java?

Validating with JSONObject String json = "Invalid_Json"; assertFalse(validator. isValid(json));

Is {} a valid JSON?

[] and {} are the minimum possible valid JSON objects. So the answer is [] and {}.

What is a valid JSON?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON. Unlike in JavaScript code in which object properties may be unquoted, in JSON only quoted strings may be used as properties.

How to check if a string is a valid JSON string?

JavaScript | Check if a string is a valid JSON string. In order to check the validity of a string whether it is a JSON string or not, We’re using the JSON.parse () method with few variations. This method parses a JSON string, constructs the JavaScript value or object specified by the string. A reviver function may be provided to do a change on ...

Why does JSON parse(12345678) return false?

However, JSON.parse ("false") evaluates to false as well. @user3651476 That's because "12345678" is a valid json string. JSON documents have a single root node, which can be null, a boolean, a number, a string, an array or an object. I know i'm 3 years late to this question, but I felt like chiming in.

Is it possible to parse a JSON string as a Boolean?

But the "false" input is a valid JSON string. This will be parsed to boolean (false). Now I modify the code to be more accurate. In prototypeJS, we have method isJSON. You can try that. Even json might help.

What is the difference between Gson and jsonparser?

The main difference of this approach is that the Gson default strategy considers separate string and numeric values to be valid as part of the JsonElement node. In other words, it considers a single string or number as valid as well. However, if we want to consider such values as malformed, we need to enforce a strict type policy on our JsonParser.


2 Answers

You can create a method to do the checking:

def valid_json?(json)     JSON.parse(json)     return true   rescue JSON::ParserError => e     return false end 
like image 101
Richa Sinha Avatar answered Sep 20 '22 15:09

Richa Sinha


You can parse it this way

begin   JSON.parse(string)   rescue JSON::ParserError => e     # do smth end   # or for method get_parsed_response  def get_parsed_response(response)   parsed_response = JSON.parse(response) rescue JSON::ParserError => e     # do smth end 
like image 41
gotva Avatar answered Sep 19 '22 15:09

gotva