Skip to content
Advertisement

Eliminate double quotes from a json field in a selective manner in PostgreSQL

I’m a new user of a PostgreSQL database with a json field which causes me some trouble. In fact, string values have been inserted into this json filed with double quote when they are supposed to be numerical values.

I have a field named “test_json” which is composed of for example these 2 rows:

I would like the 2 lines above to be after correction in this form:

I would like to correct these values without having to delete everything and I cannot correct it by hand because there are tens of thousands of “parameter1” values. Can you help me make this correction?

I would like to remove the double quotes for this parameter1 only and in case it is not numeric.

I find all the lines that have this non-numerical parameter1 with :

And I guess I have to use the update function afterwards, but I can’t manage it…

Thanks!

Franck

Advertisement

Answer

You can use jsonb_set() to turn change the datatype of the value of attribyte parameter1:

Demo on DB Fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement