Last active
March 16, 2025 23:18
-
-
Save SysProfile/8c18a8b3717abde3a5fa81af67174244 to your computer and use it in GitHub Desktop.
jSON2XML Pharser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Ejemplo de uso: | |
API.Function("SetDynamicValue1", Value:="datos") | |
API.Function("SetDynamicValue2", Value:="{""persona"":{""nombre"":""Juan"",""edad"":30}}") | |
API.Function("ScriptStart", Value:="jSON2XML") | |
Luego, para leer el resultado: | |
Dim xml As String = API.XML() | |
Dim doc As New Xml.XmlDocument() | |
doc.LoadXml(xml) | |
Dim nodo As Xml.XmlNode = doc.SelectSingleNode("//dynamic/value[@key='DynamicValue2']") | |
Dim valor As String = If(nodo IsNot Nothing, nodo.InnerText, "No encontrado") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' JSON to XML Converter for vMix (jSON2XML) | |
' Simple script to convert JSON to XML without using external libraries | |
' Usage: | |
' 1. Set root element name: API.Function("SetDynamicValue1", Value:="rootName") | |
' 2. Set JSON string/path: API.Function("SetDynamicValue2", Value:="jsonString or filePath") | |
' 3. Start conversion: API.Function("ScriptStart", Value:="jSON2XML") | |
' 4. Get result from DynamicValue2 | |
' Obtener parámetros desde los valores dinámicos | |
Dim rootName As String = "" | |
Dim inputValue As String = "" | |
Try | |
Dim xml As String = API.XML() | |
Dim doc As New Xml.XmlDocument() | |
doc.LoadXml(xml) | |
Dim nodoRootName As Xml.XmlNode = doc.SelectSingleNode("//dynamic/value[@key='DynamicValue1']") | |
If nodoRootName IsNot Nothing Then rootName = nodoRootName.InnerText | |
Dim nodoInputValue As Xml.XmlNode = doc.SelectSingleNode("//dynamic/value[@key='DynamicValue2']") | |
If nodoInputValue IsNot Nothing Then inputValue = nodoInputValue.InnerText | |
Catch ex As Exception | |
Console.WriteLine("Error: " & ex.Message) | |
End Try | |
' Usar valor por defecto si no se proporciona nombre raíz | |
If String.IsNullOrEmpty(rootName) Then rootName = "data" | |
' Salir si no hay entrada | |
If String.IsNullOrEmpty(inputValue) Then | |
API.Function("SetDynamicValue2", Value:="Error: No se proporcionó ningún JSON") | |
Exit Sub | |
End If | |
' Determinar si es archivo o cadena JSON | |
Dim jsonString As String = "" | |
Dim isFile As Boolean = False | |
Dim outputFilePath As String = "" | |
Try | |
If IO.File.Exists(inputValue) Then | |
jsonString = IO.File.ReadAllText(inputValue) | |
isFile = True | |
outputFilePath = IO.Path.ChangeExtension(inputValue, "xml") | |
Else | |
jsonString = inputValue | |
End If | |
Catch ex As Exception | |
jsonString = inputValue | |
End Try | |
' Declaración XML y variable para resultado | |
Dim xmlDeclaration As String = "<?xml version=""1.0"" encoding=""UTF-8"" ?>" | |
Dim resultado As String = xmlDeclaration & vbCrLf & "<" & rootName & ">" | |
' Índice para procesar la cadena | |
Dim index As Integer = 0 | |
' Saltar espacios iniciales | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Verificar que comience con '{' | |
If index < jsonString.Length AndAlso jsonString(index) = "{"c Then | |
index += 1 ' Saltar '{' | |
' Procesar objeto principal | |
While index < jsonString.Length | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Fin del objeto | |
If index < jsonString.Length AndAlso jsonString(index) = "}"c Then | |
Exit While | |
End If | |
' Coma entre elementos | |
If index < jsonString.Length AndAlso jsonString(index) = ","c Then | |
index += 1 | |
Continue While | |
End If | |
' Leer clave | |
If index < jsonString.Length AndAlso jsonString(index) = """"c Then | |
index += 1 | |
Dim key As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
key &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Buscar dos puntos | |
If index < jsonString.Length AndAlso jsonString(index) = ":"c Then | |
index += 1 | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Determinar tipo de valor | |
If index < jsonString.Length Then | |
Select Case jsonString(index) | |
Case "{"c ' Objeto anidado | |
resultado &= "<" & key & ">" | |
index += 1 | |
' Procesar objeto anidado | |
While index < jsonString.Length | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Fin del objeto | |
If index < jsonString.Length AndAlso jsonString(index) = "}"c Then | |
index += 1 | |
Exit While | |
End If | |
' Coma entre elementos | |
If index < jsonString.Length AndAlso jsonString(index) = ","c Then | |
index += 1 | |
Continue While | |
End If | |
' Leer clave | |
If index < jsonString.Length AndAlso jsonString(index) = """"c Then | |
index += 1 | |
Dim innerKey As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
innerKey &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Buscar dos puntos | |
If index < jsonString.Length AndAlso jsonString(index) = ":"c Then | |
index += 1 | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Determinar tipo de valor | |
If index < jsonString.Length Then | |
Select Case jsonString(index) | |
Case "{"c ' Otro objeto anidado | |
resultado &= "<" & innerKey & ">" | |
index += 1 | |
' Procesar objeto anidado (nivel 3) | |
While index < jsonString.Length | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Fin del objeto | |
If index < jsonString.Length AndAlso jsonString(index) = "}"c Then | |
index += 1 | |
Exit While | |
End If | |
' Coma entre elementos | |
If index < jsonString.Length AndAlso jsonString(index) = ","c Then | |
index += 1 | |
Continue While | |
End If | |
' Leer clave | |
If index < jsonString.Length AndAlso jsonString(index) = """"c Then | |
index += 1 | |
Dim nestedKey As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
nestedKey &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Buscar dos puntos | |
If index < jsonString.Length AndAlso jsonString(index) = ":"c Then | |
index += 1 | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Leer valor | |
If index < jsonString.Length Then | |
If jsonString(index) = """"c Then ' String | |
index += 1 | |
Dim nestedValue As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
nestedValue &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Escapar XML | |
nestedValue = nestedValue.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("""", """).Replace("'", "'") | |
resultado &= "<" & nestedKey & ">" & nestedValue & "</" & nestedKey & ">" | |
Else ' Número u otro valor | |
Dim valueText As String = "" | |
While index < jsonString.Length AndAlso Not (jsonString(index) = ","c OrElse jsonString(index) = "}"c OrElse jsonString(index) = "]"c) | |
valueText &= jsonString(index) | |
index += 1 | |
End While | |
valueText = valueText.Trim() | |
resultado &= "<" & nestedKey & ">" & valueText & "</" & nestedKey & ">" | |
End If | |
End If | |
End If | |
End If | |
End While | |
resultado &= "</" & innerKey & ">" | |
Case "["c ' Array | |
index += 1 | |
' Procesar elementos del array | |
While index < jsonString.Length | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Fin del array | |
If index < jsonString.Length AndAlso jsonString(index) = "]"c Then | |
index += 1 | |
Exit While | |
End If | |
' Coma entre elementos | |
If index < jsonString.Length AndAlso jsonString(index) = ","c Then | |
index += 1 | |
Continue While | |
End If | |
' Leer valor del array | |
If index < jsonString.Length Then | |
If jsonString(index) = """"c Then ' String en array | |
index += 1 | |
Dim arrayValue As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
arrayValue &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Escapar XML | |
arrayValue = arrayValue.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("""", """).Replace("'", "'") | |
resultado &= "<" & innerKey & ">" & arrayValue & "</" & innerKey & ">" | |
ElseIf jsonString(index) = "{"c Then ' Objeto en array | |
resultado &= "<" & innerKey & ">objeto_anidado</" & innerKey & ">" | |
' Saltar el objeto completo | |
index += 1 | |
Dim nestingLevel As Integer = 1 | |
While index < jsonString.Length AndAlso nestingLevel > 0 | |
If jsonString(index) = "{"c Then | |
nestingLevel += 1 | |
ElseIf jsonString(index) = "}"c Then | |
nestingLevel -= 1 | |
End If | |
index += 1 | |
End While | |
Else ' Número u otro valor | |
Dim valueText As String = "" | |
While index < jsonString.Length AndAlso Not (jsonString(index) = ","c OrElse jsonString(index) = "}"c OrElse jsonString(index) = "]"c) | |
valueText &= jsonString(index) | |
index += 1 | |
End While | |
valueText = valueText.Trim() | |
resultado &= "<" & innerKey & ">" & valueText & "</" & innerKey & ">" | |
End If | |
End If | |
End While | |
Case """"c ' String | |
index += 1 | |
Dim stringValue As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
stringValue &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Escapar XML | |
stringValue = stringValue.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("""", """).Replace("'", "'") | |
resultado &= "<" & innerKey & ">" & stringValue & "</" & innerKey & ">" | |
Case Else ' Número u otro valor | |
Dim valueText As String = "" | |
While index < jsonString.Length AndAlso Not (jsonString(index) = ","c OrElse jsonString(index) = "}"c OrElse jsonString(index) = "]"c) | |
valueText &= jsonString(index) | |
index += 1 | |
End While | |
valueText = valueText.Trim() | |
resultado &= "<" & innerKey & ">" & valueText & "</" & innerKey & ">" | |
End Select | |
End If | |
End If | |
End If | |
End While | |
resultado &= "</" & key & ">" | |
Case "["c ' Array en nivel superior | |
resultado &= "<" & key & ">" | |
index += 1 | |
' Simplificado: solo procesar elementos básicos del array | |
While index < jsonString.Length | |
' Saltar espacios | |
While index < jsonString.Length AndAlso Char.IsWhiteSpace(jsonString(index)) | |
index += 1 | |
End While | |
' Fin del array | |
If index < jsonString.Length AndAlso jsonString(index) = "]"c Then | |
index += 1 | |
Exit While | |
End If | |
' Coma entre elementos | |
If index < jsonString.Length AndAlso jsonString(index) = ","c Then | |
index += 1 | |
Continue While | |
End If | |
' Saltar el elemento (simplificado) | |
Dim inString As Boolean = False | |
Dim nestingLevel As Integer = 0 | |
While index < jsonString.Length | |
Dim c As Char = jsonString(index) | |
If c = """"c AndAlso (index = 0 OrElse jsonString(index - 1) <> "\"c) Then | |
inString = Not inString | |
End If | |
If Not inString Then | |
If c = "{"c OrElse c = "["c Then | |
nestingLevel += 1 | |
ElseIf c = "}"c OrElse c = "]"c Then | |
nestingLevel -= 1 | |
If nestingLevel < 0 Then | |
Exit While | |
End If | |
ElseIf c = ","c AndAlso nestingLevel = 0 Then | |
Exit While | |
End If | |
End If | |
index += 1 | |
End While | |
End While | |
resultado &= "</" & key & ">" | |
Case """"c ' String | |
index += 1 | |
Dim stringValue As String = "" | |
While index < jsonString.Length AndAlso jsonString(index) <> """"c | |
stringValue &= jsonString(index) | |
index += 1 | |
End While | |
index += 1 | |
' Escapar XML | |
stringValue = stringValue.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("""", """).Replace("'", "'") | |
resultado &= "<" & key & ">" & stringValue & "</" & key & ">" | |
Case Else ' Número u otro valor | |
Dim valueText As String = "" | |
While index < jsonString.Length AndAlso Not (jsonString(index) = ","c OrElse jsonString(index) = "}"c) | |
valueText &= jsonString(index) | |
index += 1 | |
End While | |
valueText = valueText.Trim() | |
resultado &= "<" & key & ">" & valueText & "</" & key & ">" | |
End Select | |
End If | |
End If | |
End If | |
End While | |
End If | |
' Cerrar etiqueta raíz | |
resultado &= "</" & rootName & ">" | |
' Devolver resultado | |
If isFile Then | |
Try | |
IO.File.WriteAllText(outputFilePath, resultado) | |
API.Function("SetDynamicValue2", Value:=outputFilePath) | |
Catch ex As Exception | |
API.Function("SetDynamicValue2", Value:=resultado) | |
End Try | |
Else | |
API.Function("SetDynamicValue2", Value:=resultado) | |
End If |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment