Implementation:Tensorflow Serving MakeJsonFromTensors
| Knowledge Sources | |
|---|---|
| Domains | Data_Serialization, API_Design |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Concrete tool for converting TensorProto output maps to JSON response strings, provided by the json_tensor module.
Description
MakeJsonFromTensors() converts the output tensor map from a PredictResponse into a JSON string. It dispatches to:
- MakeRowFormatJsonFromTensors() for row format ({"predictions": [...]})
- MakeColumnarFormatJsonFromTensors() for columnar format ({"outputs": {...}})
Both use AddSingleValueAndAdvance() for type-specific value serialization. The function uses RapidJSON's PrettyWriter for output formatting. Error responses are generated by MakeJsonFromStatus() which produces {"error": "<message>"}.
Usage
Called internally by ProcessPredictRequest() after inference completes. The format parameter must match the format detected during request parsing.
Code Reference
Source Location
- Repository: tensorflow/serving
- File: tensorflow_serving/util/json_tensor.cc
- Lines: L1052-1066 (MakeJsonFromTensors), L975-1026 (MakeRowFormatJsonFromTensors), L1028-1048 (MakeColumnarFormatJsonFromTensors), L888-950 (AddSingleValueAndAdvance)
- Header: tensorflow_serving/util/json_tensor.h L259-261
Signature
tensorflow::Status MakeJsonFromTensors(
const ::google::protobuf::Map<string, TensorProto>& tensor_map,
JsonPredictRequestFormat format,
string* json
);
Import
#include "tensorflow_serving/util/json_tensor.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tensor_map | Map<string, TensorProto> | Yes | Named output tensors from PredictResponse |
| format | JsonPredictRequestFormat | Yes | Must match input format (kRow or kColumnar) |
Outputs
| Name | Type | Description |
|---|---|---|
| json | string* | JSON response string |
Usage Examples
Row Format Response
{
"predictions": [
[0.1, 0.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.8, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
]
}
Columnar Format Response
{
"outputs": {
"scores": [
[0.1, 0.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.8, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
]
}
}
Error Response
{
"error": "Model mnist not found"
}