How to escape quotes from a json key in Postgresql?

0

I have a function that receives JSON's, it happens that the user can put it like this:

Normal :

'[{"nome":"João Carlos"}, {"nome":"Maria Silva"}]'

Problem :

'[{"nome":"João - "Carlos" - "}, {"nome":"Maria Silva"}]'

Note that it is possible for the user to put double or single quotes in the name field, how could I escape these quotes?

I use the JSON_ARRAY_ELEMENTS function to separate each JSON into rows.

SELECT 
    * 
FROM 
    json_array_elements('[{"nome":"João - "Carlos" - "}, {"nome":"Maria Silva"}]')
    
asked by anonymous 26.11.2018 / 16:54

1 answer

1

There's nothing wrong with your role!

This data in JSON format you are trying to process is malformed, see:

'[{"nome":"João - "Carlos" - "}, {"nome":"Maria Silva"}]'

Notice that there is no way to differentiate the quotes that should be escaped from those that should not be!

The problem is outside the scope of its function.

The caller of your function needs to be careful to pass a valid JSON as an argument when calling it.

Maybe this might help: link

    
29.11.2018 / 13:38