#!/bin/bash # JSON Formatter Tool JSON_INPUT="${FORM_DATA["json-input"]}" OPERATION="${FORM_DATA["operation"]}" if [[ -z "$JSON_INPUT" ]]; then echo "
Please enter JSON data to process
" exit 0 fi validate_json() { local json="$1" echo "$json" | python3 -m json.tool >/dev/null 2>&1 return $? } format_json() { local json="$1" echo "$json" | python3 -m json.tool 2>/dev/null } minify_json() { local json="$1" echo "$json" | python3 -c "import sys, json; print(json.dumps(json.load(sys.stdin), separators=(',', ':')))" 2>/dev/null } echo "
" echo "

JSON Formatter Results

" echo "
" echo "Original Input:
" echo "
$JSON_INPUT

" if validate_json "$JSON_INPUT"; then echo "✅ Valid JSON

" case "$OPERATION" in "format") echo "Formatted JSON:
" formatted=$(format_json "$JSON_INPUT") if [[ -n "$formatted" ]]; then echo "
$formatted
" echo "
JSON Analysis:
" echo "
" object_count=$(echo "$formatted" | grep -c '{') array_count=$(echo "$formatted" | grep -c '\[') string_count=$(echo "$formatted" | grep -o '"[^"]*"' | wc -l) number_count=$(echo "$formatted" | grep -oE '\b[0-9]+(\.[0-9]+)?\b' | wc -l) echo "📊 Structure:
" echo "• Objects: $object_count
" echo "• Arrays: $array_count
" echo "• Strings: $string_count
" echo "• Numbers: $number_count
" original_size=${#JSON_INPUT} formatted_size=${#formatted} echo "
📏 Size:
" echo "• Original: $original_size characters
" echo "• Formatted: $formatted_size characters
" echo "
" else echo "
Error formatting JSON
" fi ;; "minify") echo "Minified JSON:
" minified=$(minify_json "$JSON_INPUT") if [[ -n "$minified" ]]; then echo "
$minified
" echo "
Size Comparison:
" echo "
" original_size=${#JSON_INPUT} minified_size=${#minified} savings=$((original_size - minified_size)) percentage=$((savings * 100 / original_size)) echo "📊 Original: $original_size characters
" echo "📊 Minified: $minified_size characters
" echo "💾 Space saved: $savings characters ($percentage%)
" echo "
" else echo "
Error minifying JSON
" fi ;; "validate") echo "JSON Validation Results:
" echo "
" echo "✅ JSON is valid!

" echo "Detailed Analysis:
" structure=$(echo "$JSON_INPUT" | python3 -c " import sys, json try: data = json.load(sys.stdin) def analyze(obj, depth=0): indent = ' ' * depth if isinstance(obj, dict): print(f'{indent}Object with {len(obj)} keys:') for key in list(obj.keys())[:5]: # Show first 5 keys print(f'{indent} - {key}: {type(obj[key]).__name__}') if len(obj) > 5: print(f'{indent} ... and {len(obj)-5} more keys') elif isinstance(obj, list): print(f'{indent}Array with {len(obj)} elements') if obj: print(f'{indent} Element type: {type(obj[0]).__name__}') elif isinstance(obj, str): print(f'{indent}String: \"{obj[:50]}{'...' if len(obj) > 50 else ''}\"') elif isinstance(obj, (int, float)): print(f'{indent}Number: {obj}') elif isinstance(obj, bool): print(f'{indent}Boolean: {obj}') elif obj is None: print(f'{indent}Null') analyze(data) except Exception as e: print(f'Error: {e}') " 2>/dev/null) echo "
$structure
" echo "
" ;; esac else echo "❌ Invalid JSON

" echo "Validation Errors:
" echo "
" error_output=$(echo "$JSON_INPUT" | python3 -m json.tool 2>&1 | tail -n 5) echo "
$error_output
" echo "
Common JSON Issues:
" echo "
    " echo "
  • Missing quotes around strings
  • " echo "
  • Trailing commas after last element
  • " echo "
  • Single quotes instead of double quotes
  • " echo "
  • Unclosed brackets or braces
  • " echo "
  • Invalid escape sequences
  • " echo "
" echo "
" fi echo "
" echo "
"