Overview
The JSON precompile provides efficient JSON parsing capabilities directly on-chain, allowing smart contracts to extract specific values from JSON data without complex string manipulation. This is particularly useful for processing API responses, oracle data, and structured data from external sources. Contract Address:0x0000000000000000000000000000000000001003
Key Features
- Efficient Parsing: Gas-optimized JSON parsing on-chain
- Type-Safe Extraction: Extract data as specific types (bytes, uint256, arrays)
- Key-Based Access: Extract values using JSON key paths
- Oracle Integration: Process structured data from external APIs
Available Functions
View Functions
extractAsBytes(bytes, string) → bytes
extractAsBytes(bytes, string) → bytes
Extract a value from JSON data as bytes.Parameters:
input(bytes): The JSON data as byteskey(string): The JSON key path to extract
extractAsBytesList(bytes, string) → bytes[]
extractAsBytesList(bytes, string) → bytes[]
Extract an array of values from JSON data as bytes array.Parameters:
input(bytes): The JSON data as byteskey(string): The JSON key path to extract (must point to an array)
extractAsUint256(bytes, string) → uint256
extractAsUint256(bytes, string) → uint256
Extract a numeric value from JSON data as uint256.Parameters:
input(bytes): The JSON data as byteskey(string): The JSON key path to extract (must be numeric)
Usage Examples
- Viem
- Ethers.js
- Manual
JSON Key Path Syntax
Simple Keys
Nested Objects
Array Access
Common Use Cases
Oracle Data Processing
- Price Feeds: Extract price data from oracle responses
- Market Data: Process trading volume, market cap, and other metrics
- External APIs: Parse responses from external data sources
DeFi Protocol Integration
- Token Metadata: Extract token information from registry responses
- Pool Data: Process liquidity pool information
- Yield Data: Extract APY and reward information
Cross-Chain Data
- Chain State: Process blockchain state information
- Validator Data: Extract validator sets and staking information
- Transaction Data: Parse complex transaction metadata
Data Processing Patterns
Type-Safe Extraction
Batch Processing
Error-Resilient Parsing
Performance Optimization
Efficient Data Extraction
Batch Key Extraction
Error Handling
Common errors when using the JSON precompile:- Invalid JSON: Malformed JSON data
- Key not found: Specified key doesn’t exist in JSON
- Type mismatch: Trying to extract wrong data type
- Invalid key path: Malformed key path syntax
Security Considerations
Input Validation
- Always validate JSON data before processing
- Sanitize key paths to prevent injection attacks
- Limit JSON data size to prevent DoS attacks
Data Integrity
- Verify data sources and authenticity
- Implement checksums for critical data
- Use multiple data sources for validation