DNS wire / RFC 8427 JSON
Work with DNS messages in Ruby
Read and write DNS wire messages as RFC 8427 JSON. Inspect malformed packets while retaining diagnostics and the original bytes.
require "dns_json"
query = DNSJSON::Message.new({
"ID" => 1042, "RD" => 1, "QNAME" => "service.test.",
"QTYPE" => 28, "QCLASS" => 1
})
packet_bytes = query.to_wire
message = DNSJSON.from_wire(packet_bytes, date: Time.now)
message.qname # => "service.test."
message.malformed? # => false
message.problems # => [] (malformed input reports byte offsets and reasons)
message.to_json(profile: DNSJSON::Profile::LOGGING)
DNSJSON.parse(message.to_json).to_wire == packet_bytes # => true
Inspect packets
Decode headers, questions, and resource records, including partial messages with byte-level diagnostics.
Preserve the source
Keep original packet bytes for exact reproduction, or construct messages from structured fields.
Shape your output
Choose member profiles and exchange query/response pairs or JSON text sequences.
Installation
Add the gem to your Gemfile:
gem "dns_json"
Run bundle install, then try the quick start. Requires Ruby 3.3 or later.
For supported dependencies and optional integrations, see the installation guide.
Scope & documentation
DNS resolution, DNSSEC verification, and pcap handling are outside scope. Original message bytes take precedence during wire encoding. The privacy profile retains query names and answer addresses; it does not anonymize a message.
Read the full documentation for supported formats, examples, and API details.