I am using google protobuf’s JsonStringToMessage function from google/protobuf/json_util.h I have one json string (pod response). I want to convert/parse that json string response into kubernetes Pod message.
But, parsing is failing with error “invalid value “2023-08-11T07:37:33Z” for type type.googleapis.com/v1_25.k8s.io.apimachinery.pkg.apis.meta.v1.Time ”
Sample code -
string rawData = watch.object().raw(); // Attached image
cout << rawData << endl;
v1_25::k8s::io::api::core::v1::Pod pod;
std::string jsonError;
JsonParseOptions options;
options.ignore_unknown_fields = true;
google::protobuf::util::Status status = JsonStringToMessage(rawData, &pod, options);
if( status.ok() )
{
cout << "success" << endl;
}
else cerr << "Failed" << status.error_message() << endl;
hvauto
January 23, 2025, 9:59am
2
Use JSON processing tools like jq:
If you want to interactively test JSON queries, you can use the jq tool to extract data. For example:
bash
CopyEdit
kubectl get pods --selector=app=cassandra -o json | jq '.items[].metadata.labels.version'
To view the entire structure for better understanding:
bash
CopyEdit
kubectl get pods --selector=app=cassandra -o json | jq .
Practice with kubectl explain:
You can use the kubectl explain command to understand the structure of the Kubernetes object you want to query:
bash
CopyEdit
kubectl explain pods
kubectl explain pods.metadata
kubectl explain pods.metadata.labels
Experiment with jsonpath:
You can also practice using kubectl get with jsonpath to fine-tune your queries:
Get the list of all Pods:
bash
CopyEdit
kubectl get pods --selector=app=cassandra -o jsonpath='{.items[*]}'
Get the names of all Pods:
bash
CopyEdit
kubectl get pods --selector=app=cassandra -o jsonpath='{.items[*].metadata.name}'