Skip to content

Instantly share code, notes, and snippets.

@thanhtungka91
Last active March 7, 2025 11:16
Show Gist options
  • Save thanhtungka91/e96e2962555eb4bfec180fe16ffe5cdc to your computer and use it in GitHub Desktop.
Save thanhtungka91/e96e2962555eb4bfec180fe16ffe5cdc to your computer and use it in GitHub Desktop.
convert query to json for kibana
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
_ "os"
"regexp"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
)
func main() {
// Create a new Fyne app and window.
a := app.New()
w := a.NewWindow("Ruby-to-JSON Converter")
w.Resize(fyne.NewSize(500, 250))
// Variables to hold selected file paths.
var inputPath, outputPath string
// UI elements.
inputLabel := widget.NewLabel("Input File: None")
outputLabel := widget.NewLabel("Output File: None")
statusLabel := widget.NewLabel("Status: Waiting for input...")
// Button to select the input file.
inputButton := widget.NewButton("Select Input File", func() {
fd := dialog.NewFileOpen(func(uc fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if uc == nil {
return
}
inputPath = uc.URI().Path()
inputLabel.SetText("Input File: " + inputPath)
}, w)
// Use the storage package's file filter for Fyne v2.
fd.SetFilter(storage.NewExtensionFileFilter([]string{".txt"}))
fd.Show()
})
// Button to select the output file.
outputButton := widget.NewButton("Select Output File", func() {
fd := dialog.NewFileSave(func(uc fyne.URIWriteCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if uc == nil {
return
}
outputPath = uc.URI().Path()
outputLabel.SetText("Output File: " + outputPath)
}, w)
fd.SetFileName("output.json")
fd.Show()
})
// Button to perform the conversion.
convertButton := widget.NewButton("Convert", func() {
if inputPath == "" || outputPath == "" {
dialog.ShowInformation("Missing File", "Please select both input and output files.", w)
return
}
// Read input file.
inputData, err := ioutil.ReadFile(inputPath)
if err != nil {
dialog.ShowError(err, w)
return
}
s := string(inputData)
// Step 1: Replace Ruby hash rockets (=>) with JSON colons (:)
reRocket := regexp.MustCompile(`=>`)
s = reRocket.ReplaceAllString(s, ":")
// Step 2: Quote unquoted keys.
// This regex matches keys starting with a digit, letter, or underscore.
reKeys := regexp.MustCompile(`(?m)(^|\{|,)\s*([0-9A-Za-z_]\w*)\s*:`)
s = reKeys.ReplaceAllString(s, `$1 "$2":`)
// Optional: Replace single quotes with double quotes if necessary:
// reSingle := regexp.MustCompile(`'`)
// s = reSingle.ReplaceAllString(s, `"`)
// Validate the JSON.
var result interface{}
if err := json.Unmarshal([]byte(s), &result); err != nil {
statusLabel.SetText(fmt.Sprintf("Error parsing JSON: %v", err))
return
}
// Pretty-print the JSON.
outputData, err := json.MarshalIndent(result, "", " ")
if err != nil {
statusLabel.SetText(fmt.Sprintf("Error generating JSON: %v", err))
return
}
// Write output to the selected file.
err = ioutil.WriteFile(outputPath, outputData, 0644)
if err != nil {
statusLabel.SetText(fmt.Sprintf("Error writing output: %v", err))
return
}
statusLabel.SetText("Conversion successful!")
})
// Layout the UI components vertically.
content := container.NewVBox(
inputLabel,
inputButton,
outputLabel,
outputButton,
convertButton,
statusLabel,
)
w.SetContent(content)
w.ShowAndRun()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment