Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
Reliably ingesting a CSV file where a value can be a JSON string?

I am parsing CSV files where values include strings that represent JSON objects, among boolean, normal strings, and other values. The CSV file has a header. As I iterate over the non-header rows of the CSV file, I use Javascript's split method with the following regex to grab the value at each 'cell' of the CSV row:

let currentLine = lines[i].split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)

Unfortunately, it doesn't always delimit lines containing legal JSON strings. Some strings are captured, but strings like this one gets truncated in odd places, wrecking the parsing of the entire CSV file:

'{"an object" : [{"sub-object 1": {
    "description": "sub-object is for blah blah",
    "nestedArray": ["param1","param2","param3"]}},
  {"sub-object 2": {
    "description": "sub-object is for blah blah",
    "nestedArray": ["param1","param2","param3"]}},
  {"sub-object 3": {
    "description": "sub-object is for blah blah",
    "nestedArray": ["param1","param2","param3"]}}
  ]
}'

As far as I can tell, the above is legal JSON (curiously, https://jsonformatter.curiousconcept.com/# validates the above if it's pasted without single quotes surrounding it -- not sure why that would be). Any thoughts on how to properly parse this? I thought this would be a fairly trivial task but apparently JSON is not frequently embedded as a value in CSV files. Not sure if there's a standard or best practices way of handling this.

EDIT: To clarify, if anyone has a regex that would capture the above json string in a CSV file, I'd be greatly appreciated if you could share it with me

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This can be used as a template for general purpose CSV parsing.

(?:(?:^|,|\r?\n)[^\S\r\n]*)(?:("[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'|[^,\r\n]*)(?:[^\S\r\n]*(?=$|,|\r?\n)))

test: https://regex101.com/r/AnQqyv/1
Where the field is trimmed in capture group 1

 (?:                           # Delimiter comma or newline
    (?: ^ | , | \r? \n )
    [^\S\r\n]*                    # leading optional whitespaces
 )
 (?:
    (                             # (1 start), field
       "                             # " Quoted
       [^"\\]* 
       (?: \\ [\S\s] [^"\\]* )*
       "
     |                              # or
       '                             # ' Quoted
       [^'\\]* 
       (?: \\ [\S\s] [^'\\]* )*
       '
     |                              # or
       [^,\r\n]*                     # Non-quoted
    )                             # (1 end)
    (?:
       [^\S\r\n]*                    # trailing optional whitespaces 
       (?= $ | , | \r? \n )          # Delimiter ahead, comma or newline
    )
 )
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda