I'm converting my Excel table to Json with VBA.
But, when I look at the output Json file, the turkish characters don't look smooth.
For example,
in Excel table, "HAYRETTIN YILMAZ"
in Json, HAYRETTÝN YILMAZ
in Excel table, "HÜSEYİN DURAK"
in Json, HÜSEYÝN DURAK
How can I fix it?
You can find my VBA code below:
Sub deneme()
savename = "deneme.js"
Dim wkb As Workbook
Dim wks As Worksheet
Set wkb = ThisWorkbook
Set wks = wkb.Sheets(2)
lcolumn = wks.Cells(1, Columns.Count).End(xlToLeft).Column ' Var olan sütunun en sonu
lrow = wks.Cells(Rows.Count, "A").End(xlUp).Row ' Var olan satırın en sonu
Dim titles() As String
ReDim titles(lcolumn)
For i = 1 To lcolumn
titles(i) = wks.Cells(1, i)
Next i
wks.Columns(50).ClearContents
json = "var deneme = { " & vbCrLf
dq = """"
m = 1
For i = 2 To ActiveSheet.Range("a1048576").End(3).Row
If WorksheetFunction.CountIf(wks.Columns("a"), wks.Cells(i, 1)) = 1 Then
json = json & vbCrLf & dq & wks.Cells(i, 1) & dq & ": {" & vbCrLf
For k = 1 To lcolumn
cellvalue = wks.Cells(i, k)
json = json & dq & titles(k) & dq & ":" & dq & cellvalue & dq
If k <> lcolumn Then ' Son sütun değilse
json = json & "," & vbCrLf
ElseIf k = lcolumn Then
json = json & vbCrLf & "}," & vbCrLf
End If
Next k
'json = json & dq & wks.Cells(1, 1) & dq & ":" & dq & wks.Cells(i, 1) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 2) & dq & ":" & dq & wks.Cells(i, 2) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 3) & dq & ":" & dq & wks.Cells(i, 3) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 4) & dq & ":" & dq & wks.Cells(i, 4) & dq & vbCrLf & "}," & vbCrLf
Else
If wks.Cells(i, 50) = "" Then
For j = i To ActiveSheet.Range("a1048576").End(3).Row
If wks.Cells(j, 1) = wks.Cells(i, 1) Then
If j = i Then
json = json & dq & wks.Cells(i, 1) & dq & ": [{" & vbCrLf
For k = 1 To lcolumn
cellvalue = wks.Cells(i, k)
json = json & dq & titles(k) & dq & ":" & dq & cellvalue & dq
If k <> lcolumn Then ' Son sütun değilse
json = json & "," & vbCrLf
ElseIf k = lcolumn Then
json = json & vbCrLf & "},"
End If
Next k
'json = json & dq & wks.Cells(1, 1) & dq & ":" & dq & wks.Cells(j, 1) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 2) & dq & ":" & dq & wks.Cells(j, 2) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 3) & dq & ":" & dq & wks.Cells(j, 3) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 4) & dq & ":" & dq & wks.Cells(j, 4) & dq & vbCrLf & "},"
Else
json = json & vbCrLf & "{" & vbCrLf
For k = 1 To lcolumn
cellvalue = wks.Cells(i, k)
json = json & dq & titles(k) & dq & ":" & dq & cellvalue & dq
If k <> lcolumn Then ' Son sütun değilse
json = json & "," & vbCrLf
ElseIf k = lcolumn Then
json = json & vbCrLf & "},"
End If
Next k
'json = json & dq & wks.Cells(1, 1) & dq & ":" & dq & wks.Cells(j, 1) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 2) & dq & ":" & dq & wks.Cells(j, 2) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 3) & dq & ":" & dq & wks.Cells(j, 3) & dq & "," & vbCrLf
'json = json & dq & wks.Cells(1, 4) & dq & ":" & dq & wks.Cells(j, 4) & dq & vbCrLf & "},"
End If
wks.Cells(j, 50) = 1
End If
Next j
json = Left(json, Len(json) - 1) & "]," & vbCrLf
End If
End If
Next i
json = Left(json, Len(json) - 3) & vbCrLf & "}" & vbCrLf & "}"
myFile = "C:\Users\xxx\Desktop\" & savename
Open myFile For Output As #1
Print #1, json
Close #1
End Sub
@PeterT, I cant want to use JsonConverter, because policy of our company. Therefore, i wrote my code above. While i was using my code for Excel to Json, "HAYRETTIN YILMAZ" seems to be "HAYRETTÝN YILMAZ".
In Addition, your 3rd item was changed. "HÜSEYİN DURAK" -> "H\u00DCSEY\u0130N DURAK". JsonConverter has a same problem.
How can i fix it ?
Thanks.
I ran a test with JsonConverter and achieved the results below. Is this different than what you're getting?
Option Explicit
Sub deneme()
Dim topLevel As Dictionary
Set topLevel = New Dictionary
topLevel.Add "Item1", Cells(1, 1).Value
topLevel.Add "Item2", Cells(1, 2).Value
topLevel.Add "Item3", Cells(2, 1).Value
topLevel.Add "Item4", Cells(2, 2).Value
Dim json As String
json = ConvertToJson(JsonValue:=topLevel, Whitespace:=2)
Debug.Print json
End Sub
Generates the following JSON:
{
"Item1": "HAYRETTIN YILMAZ",
"Item2": "HAYRETT\u00DDN YILMAZ",
"Item3": "H\u00DCSEY\u0130N DURAK",
"Item4": "H\u00DCSEY\u00DDN DURAK"
}