-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathjson-to-yaml.html
197 lines (174 loc) · 4.25 KB
/
json-to-yaml.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON to YAML Converter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
margin: 0;
padding: 20px;
background: #f5f5f5;
max-width: 800px;
margin: 0 auto;
}
.panel {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
textarea {
width: 100%;
height: 200px;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: monospace;
font-size: 16px;
resize: vertical;
line-height: 1.4;
}
.output-section {
margin-top: 16px;
}
.output-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
button {
background: #2563eb;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #1d4ed8;
}
.error {
color: #dc2626;
margin-top: 8px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="panel">
<label for="jsonInput">JSON Input</label>
<textarea id="jsonInput" placeholder="Paste your JSON here..."></textarea>
<div id="error" class="error"></div>
</div>
<div class="panel">
<div class="output-section">
<div class="output-header">
<label>Block Style YAML</label>
<button data-copy="block">Copy</button>
</div>
<textarea id="blockOutput" readonly></textarea>
</div>
<div class="output-section">
<div class="output-header">
<label>Flow Style YAML (Compact)</label>
<button data-copy="flow">Copy</button>
</div>
<textarea id="flowOutput" readonly></textarea>
</div>
<div class="output-section">
<div class="output-header">
<label>Quoted Strings YAML</label>
<button data-copy="quote">Copy</button>
</div>
<textarea id="quoteOutput" readonly></textarea>
</div>
</div>
<script type="module">
const jsonInput = document.getElementById('jsonInput')
const blockOutput = document.getElementById('blockOutput')
const flowOutput = document.getElementById('flowOutput')
const quoteOutput = document.getElementById('quoteOutput')
const errorDiv = document.getElementById('error')
function convertToYaml() {
const jsonStr = jsonInput.value
errorDiv.textContent = ''
if (!jsonStr.trim()) {
blockOutput.value = ''
flowOutput.value = ''
quoteOutput.value = ''
return
}
try {
const jsonObj = JSON.parse(jsonStr)
// Block style
blockOutput.value = jsyaml.dump(jsonObj, {
indent: 2,
noArrayIndent: true
})
// Flow style (compact)
flowOutput.value = jsyaml.dump(jsonObj, {
flowLevel: 1,
noArrayIndent: true
})
// Quoted strings
quoteOutput.value = jsyaml.dump(jsonObj, {
styles: {
'!!str': 'double'
},
noArrayIndent: true
})
} catch (err) {
errorDiv.textContent = 'Invalid JSON: ' + err.message
}
}
jsonInput.addEventListener('input', convertToYaml)
document.querySelectorAll('button[data-copy]').forEach(button => {
button.addEventListener('click', async () => {
const style = button.dataset.copy
const output = document.getElementById(style + 'Output')
try {
await navigator.clipboard.writeText(output.value)
const originalText = button.textContent
button.textContent = 'Copied!'
setTimeout(() => {
button.textContent = originalText
}, 2000)
} catch (err) {
errorDiv.textContent = 'Failed to copy: ' + err.message
}
})
})
// Add some example JSON on load
jsonInput.value = `{
"models": [
{
"provider": "gemini",
"model_id": "gemini-1.5-flash",
"tiers": [
{
"max_tokens": 128000,
"input_cost": 7,
"output_cost": 30
}
]
}
]
}`
convertToYaml()
</script>
</body>
</html>