Convert gRPC Response to JSON: Step-by-Step Guide
In this article
To convert a gRPC response to JSON, you need to capture the protobuf message, decode it with the matching .proto schema, and serialize the resulting object to JSON. In practice that means using a tool that understands protobuf—such as grpcurl, Postman, or a language library like protobufjs or google-protobuf—rather than treating the raw bytes as text. This guide walks through the reliable methods, the schema requirements, and the traps that produce empty or garbled output.
What You'll Need
- The .proto file (or files) that define the service and message types, including any imported files.
- Network access to the gRPC server and the server address plus port.
- One of: grpcurl installed, Postman with gRPC support, or a programming language with a protobuf runtime (Node.js, Python, Go, Java, C#).
- For authenticated services, the token, API key, or client certificate the server expects.
- If the server uses server reflection, you can skip the .proto files; otherwise they are mandatory.
Step-by-Step
- Confirm the service and method names. Open the .proto file and note the package, service name, and the exact RPC method you are calling. For example, a service
UserServicein packageusers.v1with methodGetUseris addressed asusers.v1.UserService/GetUser. Getting this string wrong is the most common cause of a failed call. - Check whether the server supports reflection. Run
grpcurl -plaintext <host>:<port> list. If it prints service names, reflection is on and you can call methods without local .proto files. If it returns an error about reflection not being supported, you must supply the .proto files with-protoand-import-pathflags. - Make the call and request JSON output. grpcurl emits JSON by default. A typical call looks like
grpcurl -plaintext -d '{"id":"123"}' <host>:<port> users.v1.UserService/GetUser. The response printed to the terminal is already JSON. Add-format jsonif you want to be explicit, or-format textto see the protobuf text format instead. - Save the output to a file. Redirect stdout:
grpcurl ... > response.json. Do not redirect stderr, because grpcurl writes errors and status messages there; mixing the two produces invalid JSON. If you need both, capture stderr separately with2> error.log. - If you are using Postman, import the .proto and invoke the method. Create a new gRPC request, enter the server URL, and import your .proto file (or use server reflection if the server exposes it). Select the service and method, fill in the request message, and send. Postman displays the response in a structured view; use the response pane's code or raw view to copy the JSON. Labels in Postman can differ slightly by version, so look for the gRPC request type and the message editor rather than relying on exact menu names.
- If you are writing code, decode then serialize. In Node.js with
@grpc/proto-loaderandgrpc-js, load the proto, call the method, and pass the response object toJSON.stringify. In Python withgrpcioandprotobuf, usegoogle.protobuf.json_format.MessageToJson(response), which correctly handles field names, enums, and 64-bit integers. In Go, useprotojson.Marshalfromgoogle.golang.org/protobuf/encoding/protojson. In Java, useJsonFormat.printer().print(message). These library functions are preferable to hand-rolled serialization because they follow the protobuf JSON mapping. - Validate the JSON. Pipe the file through a parser, for example
python -m json.tool response.jsonorjq . response.json. If parsing fails, the output likely contains a status line, a warning, or truncated data rather than pure JSON. - Handle field naming. By default, protobuf JSON uses lowerCamelCase field names even if the .proto uses snake_case. If your consumer expects the original names, grpcurl supports
-emit-defaultsfor zero values but not a naming switch; in code, the JSON formatter options let you preserve proto field names. Check the consumer's expectations before shipping.
Common Mistakes
- Treating raw gRPC bytes as JSON. gRPC messages are binary protobuf on the wire. Reading them as text produces control characters and invalid JSON. Always decode with the schema first.
- Using a mismatched .proto version. If the client schema is older than the server's, unknown fields are dropped and the JSON looks incomplete. Pull the current .proto from the service owner.
- Forgetting imports. A .proto that imports
google/protobuf/timestamp.protowill fail to compile unless the import path is supplied. grpcurl needs-import-pathpointing at the directory containing the well-known types. - Mixing stderr into the JSON file. Errors and status messages go to stderr; only stdout is the response.
- Assuming 64-bit integers survive as numbers. The protobuf JSON mapping represents 64-bit integers as strings to avoid precision loss. Consumers that expect numbers must convert them deliberately.
- Ignoring authentication. A call without the required metadata returns an error, not a response, and the error text is not JSON.
Tips
- Use server reflection where available; it removes the need to keep .proto files in sync.
- Prefer the official JSON formatter in your language runtime over generic object serialization, because it handles enums, bytes (base64), timestamps (RFC 3339 strings), and default value omission correctly.
- Keep a copy of the .proto files under version control alongside the client code so schema drift is visible.
- For debugging,
grpcurl describeshows the message shape without making a call. - When piping to
jq, remember that a streaming RPC returns multiple JSON objects, one per line; usejq -sto combine them into an array.
Troubleshooting
If the call fails with a connection error, verify the host, port, and whether the server expects TLS. Plaintext servers need -plaintext; TLS servers need the certificate or -insecure only in test environments. If you get an "unknown service" error, the method path is wrong or reflection is returning a different package name than you expect. If the response is empty, check whether the method is server-streaming and whether you are reading all messages. If JSON parsing fails, inspect the first bytes of the file for a status prefix or a warning line. If fields are missing, compare the .proto versions and confirm the server is not using a newer schema. If authentication fails, confirm the metadata key and value format the server documents; some servers expect a bearer token, others a raw API key.
Frequently Asked Questions
Can I convert a gRPC response to JSON without the .proto file?
Only if the server supports reflection, which lets tools discover the schema at runtime. Otherwise the .proto file is required to decode the binary message.
Why are my 64-bit numbers quoted in the JSON?
The protobuf JSON mapping encodes 64-bit integers as strings to prevent precision loss in environments where numbers are doubles. Convert them in the consumer if needed.
Does grpcurl output JSON by default?
Yes. grpcurl prints responses in JSON format unless you request text format with -format text.
How do I convert a streaming response?
Each message in a stream is emitted as a separate JSON object. Collect them into an array or process them line by line; do not assume a single JSON document.
Why does my JSON contain different field names than the .proto?
Protobuf JSON uses lowerCamelCase by default. Formatter options in most runtimes let you preserve the original proto field names if a consumer requires them.
Can I use this in a browser?
Browsers cannot speak gRPC directly over HTTP/2 in the standard way; you need a gRPC-Web proxy or a server that exposes gRPC-Web. The JSON conversion step is the same once you have the decoded message.