I found many answers for parsing JSON or JSONB but it seems PostgreSQL has its own way of doing things.
I have a CSV dump from PostgreSQL that I want to parse in Ruby.
A column of the dump was made with array_agg. It is an array of strings. Unfortunately, PostgreSQL decided not to double-quote some values. It may believe they are numbers.
require 'json'
original_names = "[\"1800 Reposado 750ML\",1800Jalisco750ml,\"1800 Tequila Reposado, Jalisco, Mexico (750ml)\"]"
array = JSON.parse original_names
# Raises: 434: unexpected token at 'Jalisco750ml,"...' (JSON::ParserError)
I have tried to add the quotes myself, but it fails because of the other string that contain commas.
array = JSON.parse original_names.gsub(/,([^"|,]+),/, ',"\1",')
# Raises: 434: unexpected token at 'Jalisco", Mexico (750ml)"]' (JSON::ParserError)
I spent an hour trying workarounds and now I believe I need to implement the parser myself. Does anyone have a better way?
1800Jalisco750mlmust be wrapped within quotes