Encode JSON Data in a QR Code
Generate a QR code containing JSON-formatted data for machine-to-machine communication. Covers JSON size limits, minification strategies, compression techniques, and practical IoT use cases.
Detailed Explanation
QR Codes for JSON Data
Encoding JSON data in a QR code enables machine-readable data transfer without network connectivity. This is useful for device provisioning, configuration sharing, and IoT scenarios.
Basic JSON QR Code
Any valid JSON can be encoded as a QR code:
{"id":"device-001","config":{"mode":"production","interval":30}}
The QR scanner reads the raw text, and the receiving application parses it as JSON.
Size Optimization
JSON is verbose by nature, so minimizing data size is critical for QR codes:
1. Minify the JSON — Remove all whitespace:
{"id":"device-001","config":{"mode":"production","interval":30}}
2. Use short key names — Replace descriptive keys with abbreviated ones:
{"i":"d001","c":{"m":"prod","t":30}}
3. Remove unnecessary fields — Only include data that the receiving application actually needs.
4. Use arrays instead of objects — When the schema is known on both sides:
["d001","prod",30]
Data Capacity Planning
For a Version 10 QR code (57x57 modules) with Medium error correction:
| Content Type | Approximate Capacity |
|---|---|
| ASCII JSON | ~652 characters |
| UTF-8 JSON | ~450 characters |
| Numeric-heavy JSON | ~1,000+ characters |
Validation Strategy
Since QR codes can be damaged or partially unreadable, always validate the parsed JSON on the receiving end:
- Check that the scanned text is valid JSON (
JSON.parse()) - Validate against an expected schema
- Verify required fields are present
- Check data types and value ranges
Alternative: Base64-Encoded Binary
For larger datasets, consider encoding binary formats (MessagePack, CBOR, Protocol Buffers) as Base64 within the QR code. This can reduce data size by 30-50% compared to plain JSON.
Security Considerations
- Never trust QR-scanned JSON without validation
- Sanitize string values to prevent injection attacks
- Use schema validation (JSON Schema) for critical applications
- Consider signing the JSON payload with HMAC if integrity is important
Use Case
JSON QR codes are used in IoT device provisioning, equipment configuration transfers, inventory management systems, manufacturing quality control checkpoints, lab sample tracking, and any scenario where structured data needs to be transferred between devices without network connectivity.