0

I'm trying to parse json with jq

{
    "xxx": {
        "aliases": {
            "business_event": {
                "is_write_index": true
            }
        },
        "mappings": {
            "business_event_doc": {
                "properties": {
                    "clientName": {
                        "type": "keyword"
                    },
                    "componentName": {
                        "type": "keyword"
                    },
                    "correlationId": {
                        "type": "keyword"
                    },
                    "executionTime": {
                        "type": "long"
                    },
                    "fullDescription": {
                        "type": "text"
                    },
                    "shortDescription": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

I need to construct an list of properties where type == "text", so that result would look like:

"fullDescription": {
    "type": "text"
},
"shortDescription": {
    "type": "text"
}

I've tried to use selectors, but the result is invalid

.xxx.mappings[].properties | select (.[].type=="text")

What is the correct way of quering child node and returning parent?

  • .xxx.mappings[].properties | to_entries[] | select(.value.type=="text") | .key Worked for me (I needed keys anyway). Still looks too complicated – Ermintar May 8 at 16:21
0

if you open to consider alternatives, then here's one based on walk-path unix utility jtc:

bash $ <file.json jtc -w'[type]:<text>:[-1]' -l
"fullDescription": {
   "type": "text"
}
"shortDescription": {
   "type": "text"
}
bash $ 

Walk-path (-w) is rather simple here:

  • [type]:<text>: - will find recursively each (all) of "type": "text", then from found entries
  • [-1] - will back up 1 level (up the Json tree), effectively selecting its parent

-l instructs to print a label for the walked entry

PS> Disclosure: I'm the creator of the jtc tool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.