Home > Software design >  jq parse json with key which has special character DOT (.)
jq parse json with key which has special character DOT (.)

Time:07-20

I have a json

{ "fundingMoved": {
    "com.mytest.map": {
        "map": {
            "java.util.HashMap": {
                "AGREEMENT_OWNING_ORG": "ABC 22",
                "WAREHOUSE": "XYZ 6"
            }
        }
    }
  }
}

I am trying to write a script to see if AGREEMENT_OWNING_ORG contains ABC and WAREHOUSE contains XYZ

My script so far

grep . jk_raw | jq -r ".fundingMoved."com.mytest.map""

This is returning null but

grep . jk_raw | jq -r ".fundingMoved

This returns result .

Issue . Due to the Dot (.) in key it is not returning the response

I checked this related post How to use jq when the variable has reserved characters?

This is suggesting to use double quote. I tried that but still response is null.

I tried to escape character . as . that doesnt help either

How to extract value from this

CodePudding user response:

I am trying to write a script to see if AGREEMENT_OWNING_ORG contains ABC and WAREHOUSE contains XYZ

Please, please, don't use grep for such tasks. Rather, consider something along these lines:

< jk_raw jq '.. | objects | select(has("java.util.HashMap"))."java.util.HashMap"
   | (.AGREEMENT_OWNING_ORG | test("ABC")) and (.WAREHOUSE | test("XYZ"))'
  • Related