Last active
August 30, 2021 15:42
-
-
Save jkehelwala/f764aaf4d778509d884315a827c84aae to your computer and use it in GitHub Desktop.
Quickly Minify and Beautify JSON Content
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
#!/usr/bin/python | |
import sys | |
import json | |
from PyQt5 import QtCore, QtWidgets | |
from PyQt5.QtCore import QSize, QPoint, Qt | |
from PyQt5.QtWidgets import QMainWindow, QLabel, QWidget, QDesktopWidget, QHBoxLayout, QVBoxLayout, qApp, \ | |
QPlainTextEdit, QPushButton | |
def json_copy(txtEdit): | |
txtEdit.selectAll() | |
txtEdit.copy() | |
class JsonCycleWindow(QMainWindow): | |
def __init__(self): | |
QMainWindow.__init__(self) | |
self.setMinimumSize(QSize(200, 400)) | |
self.setWindowTitle("Json Cycle") | |
centralWidget = QWidget(self) | |
self.setCentralWidget(centralWidget) | |
gridLayout = QHBoxLayout(self) | |
centralWidget.setLayout(gridLayout) | |
self.jsonMini = QPlainTextEdit(self) | |
self.jsonBeauti = QPlainTextEdit(self) | |
btnMinify = QPushButton("Minify") | |
btnBeautify = QPushButton("Beautify") | |
btnMinify.clicked.connect(lambda: self.minify_json_content()) | |
btnBeautify.clicked.connect(lambda: self.beautify_json_content()) | |
minifyLayout = QVBoxLayout(self) | |
minifyLayout.addWidget(QLabel("Minified JSON")) | |
minifyLayout.addWidget(self.jsonMini) | |
minifyLayout.addWidget(btnBeautify) | |
beautifyLayout = QVBoxLayout(self) | |
beautifyLayout.addWidget(QLabel("Beautified JSON")) | |
beautifyLayout.addWidget(self.jsonBeauti) | |
beautifyLayout.addWidget(btnMinify) | |
gridLayout.addLayout(minifyLayout) | |
gridLayout.addLayout(beautifyLayout) | |
def minify_json_content(self): | |
y = json.loads(self.jsonBeauti.toPlainText()) | |
self.jsonMini.setPlainText(json.dumps(y, separators=(',', ':'))) | |
json_copy(self.jsonMini) | |
def beautify_json_content(self): | |
y = json.loads(self.jsonMini.toPlainText()) | |
self.jsonBeauti.setPlainText(json.dumps(y, indent=4)) | |
json_copy(self.jsonBeauti) | |
if __name__ == "__main__": | |
app = QtWidgets.QApplication(sys.argv) | |
mainWin = JsonCycleWindow() | |
mainWin.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For cycling between formats for text formatting in reports