Warn during json-tool extraction if the extracted field isn't found

This commit is contained in:
Eric Fischer 2017-11-17 13:52:45 -08:00
parent b6163dd435
commit 2b1cba0b53

@ -138,9 +138,11 @@ std::string sort_quote(const char *s) {
void out(std::string const &s, int type, json_object *properties) {
if (extract != NULL) {
std::string extracted = sort_quote("null");
bool found = false;
json_object *o = json_hash_get(properties, extract);
if (o != NULL) {
found = true;
if (o->type == JSON_STRING || o->type == JSON_NUMBER) {
extracted = sort_quote(o->string);
} else {
@ -153,6 +155,14 @@ void out(std::string const &s, int type, json_object *properties) {
}
}
if (!found) {
static bool warned = false;
if (!warned) {
fprintf(stderr, "Warning: extract key \"%s\" not found in JSON\n", extract);
warned = true;
}
}
printf("{\"%s\":%s}\n", extracted.c_str(), s.c_str());
return;
}