-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJSON_Data_Formatter.go
65 lines (54 loc) · 1.19 KB
/
JSON_Data_Formatter.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
)
// Function to format JSON data
func formatJSON(input []byte) (string, error) {
var formattedJSON bytes.Buffer
err := json.Indent(&formattedJSON, input, "", " ")
if err != nil {
return "", err
}
return formattedJSON.String(), nil
}
func main() {
// Read JSON input from standard input
fmt.Println("Enter JSON data:")
var input []byte
_, err := fmt.Scanln(&input)
if err != nil {
fmt.Println("Error reading input:", err)
os.Exit(1)
}
// Format JSON
output, err := formatJSON(input)
if err != nil {
fmt.Println("Error formatting JSON:", err)
os.Exit(1)
}
// Print formatted JSON
fmt.Println("Formatted JSON:")
fmt.Println(output)
}
// SAMPLE INPUT OUTPUT AND EXPLANATION
// ---
// JSON Formatter Example
// Input:
// {"name":"John","age":30,"city":"New York","skills":["Go","Python","JavaScript"]}
// Output:
// {
// "name": "John",
// "age": 30,
// "city": "New York",
// "skills": [
// "Go",
// "Python",
// "JavaScript"
// ]
// }
// Explanation:
// -Input: A single-line JSON string with no formatting.
// -Output: The JSON formatted with indentation and line breaks, making it easy to read.