I have a column in Excel where I have downloaded data, I would like to create a macro that would take that initial column of data in JSON and then return new columns of data where the information is correctly separated. I would like that the order of the new columns wolud be the following one:
**id Codi_estacio Codi_variable Data_tectura Valor_lectura Codi_base**
X9320111230000 X9 32 2023-11-01T00:00:00.000 8 SH
. . . . . .
. . . . . .
. . . . . .
I tried to create a macro that returns the new ordered columns next to the original using a library of jsonconverter that I found on internet, but I'm having some mistakes with the library. I downladed the necessary references in order to apply that code
My code:
Sub ProcesarColumnaJSON()
Dim columnaOriginal As Range
Dim celda As Range
Dim datosJSON As Collection
Dim resultado As Variant
Dim i As Integer
Dim filaResultado As Integer
Set columnaOriginal = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
filaResultado = 1
For Each celda In columnaOriginal
Set datosJSON = JsonConverter.ParseJson(celda.Value)
ReDim resultado(1 To 1, 1 To datosJSON.Count)
i = 1
For Each key In datosJSON
resultado(1, i) = datosJSON(key)
i = i + 1
Next key
Range(Cells(filaResultado, 2), Cells(filaResultado, UBound(resultado, 2) + 1)).Value = resultado
filaResultado = filaResultado + 1
Next celda
End Sub
JsonConverter
is a powerful tool.Dictionary
class module is necessary.Split
is a good option too.Microsoft documentation: