0
I have a function that receives JSON’s, it turns out that it is possible for the user to put as follows:
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 place double or single quotes in the field name, how could I escape these quotes?
I use the function JSON_ARRAY_ELEMENTS
to separate each JSON into rows.
SELECT
*
FROM
json_array_elements('[{"nome":"João - "Carlos" - "}, {"nome":"Maria Silva"}]')
have you tried using scape or repeated double quotes? type
json_array_elements('[{"nome":"João - \"Carlos\" - "}, {"nome":"Maria Silva"}]')
orjson_array_elements('[{"nome":"João - ""Carlos"" - "}, {"nome":"Maria Silva"}]')
– Adriano Gomes
@Adrianogomes Until I thought, but I as I could put these bars inverted?
– D. Watson
vc is using some programming language to run sql script?
– Adriano Gomes
Node.JS to run the postgresql function that receives JSON and inserts into table.
– D. Watson
Instead of using the json_array_elements function and using only Node to create this list is an option?
– Adriano Gomes
The problem is that the json format is invalid, you need to inform the backslash when there are double quotes in a string, as @Adrianogomes suggested. If you don’t do this you won’t be able to parse. An alternative would be to process the data informed by the user by adding the bars.
– Camilo Santos
In Node I could only do it using instead of a backslash, two, thus staying:
var str = '[{"nome":"João - \\"Carlos\\" - "}, {"nome":"Maria Silva"}]';
var obj = JSON.parse(str);
console.log(obj);
– Adriano Gomes