Skip to main content
Glama
JSONFormatting.swift1.3 kB
import Foundation // MARK: - JSON Formatting Helpers /// Format JSON for pretty printing with optional indentation public func formatJSON(_ jsonString: String, indent: String = " ") -> String? { guard let data = jsonString.data(using: .utf8), let json = try? JSONSerialization.jsonObject(with: data), let prettyData = try? JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted, .sortedKeys]), let prettyString = String(data: prettyData, encoding: .utf8) else { return nil } // Add indentation to each line return prettyString .split(separator: "\n") .map { indent + $0 } .joined(separator: "\n") } /// Parse JSON string arguments into a dictionary public func parseArguments(_ arguments: String) -> [String: Any] { guard let data = arguments.data(using: .utf8), let args = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return [:] } return args } /// Parse JSON result string into a dictionary public func parseResult(_ rawResult: String) -> [String: Any]? { guard let data = rawResult.data(using: .utf8), let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil } return json }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/steipete/Peekaboo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server