Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent 'invalid input syntax for type json' in Postgres, when records contain a mix of json or strings

Tags:

I have a text column that contains JSON and also plan text. I want to convert it to JSON, and then select a particular property. For example:

user_data _________ {"user": {"name": "jim"}} {"user": {"name": "sally"}} some random data string 

I've tried:

select user_data::json#>'{user,name}' from users 

I get:

ERROR:  invalid input syntax for type json DETAIL:  Token "some" is invalid. CONTEXT:  JSON user_data, line 1: some... 

Is it possible to prevent this?

like image 793
Allyl Isocyanate Avatar asked Aug 31 '17 01:08

Allyl Isocyanate


People also ask

Does Postgres support JSON data type?

PostgreSQL offers two types for storing JSON data: json and jsonb . To implement efficient query mechanisms for these data types, PostgreSQL also provides the jsonpath data type described in Section 8.14. 7. The json and jsonb data types accept almost identical sets of values as input.

Can we insert JSON data into PostgreSQL?

PostgreSQL allows you to work on both JSON and SQL data to implement relational and non-relational queries. This way, PostgreSQL allows you to leverage SQL commands for processing data stored in tables of respective database servers.

Can you store any string in a JSON Postgres data type?

You can save any valid json value to either json or to a jsonb column. But you cannot bind it as string/ text / varchar , if you use prepared statements (use casts instead in sql, like UPDATE ... SET json_col = $1::json or bind it as unknown ).

What is JSON data type in PostgreSQL?

Another data type in PostgreSQL is JSON, which stands for JavaScript Object Notation. It is an open-standard format that contains key-value pairs. The main objective of using the JSON data type is to transfer data between a server and a web application. JSON is human-readable text distinct from the other formats.


1 Answers

If you want to skip the rows with invalid JSON, you must first test if the text is valid JSON. You can do this by creating a function which will attempt to parse the value, and catch the exception for invalid JSON values.

CREATE OR REPLACE FUNCTION is_json(input_text varchar) RETURNS boolean AS $$   DECLARE     maybe_json json;   BEGIN     BEGIN       maybe_json := input_text;     EXCEPTION WHEN others THEN       RETURN FALSE;     END;      RETURN TRUE;   END; $$ LANGUAGE plpgsql IMMUTABLE; 

When you have that, you could use the is_json function in a CASE or WHERE clause to narrow down the valid values.

-- this can eliminate invalid values SELECT user_data::json #> '{user,name}' FROM users WHERE is_json(user_data);  -- or this if you want to fill will NULLs SELECT   CASE     WHEN is_json(user_data)       THEN user_data::json #> '{user,name}'     ELSE       NULL   END FROM users; 
like image 179
Andy Carlson Avatar answered Sep 18 '22 22:09

Andy Carlson