1

I am working with a JSON file similar to the one below:

{   "Response" : {
    "TimeUnit" : [ 1576126800000 ],
    "metaData" : {
      "errors" : [ ],
      "notices" : [ "query served by:1"]
    },
    "stats" : {
      "data" : [ {
        "identifier" : {
          "names" : [ "apiproxy", "response_status_code", "target_response_code", "target_ip" ],
          "values" : [ "IO", "502", "502", "7.1.143.6" ]
        },
        "metric" : [ {
          "env" : "dev",
          "name" : "sum(message_count)",
          "values" : [ 0.0]
        } ]
      } ]
    }   } }

My object is to display a mapping of the identifier and values like :

apiproxy=IO
response_status_code=502
target_response_code=502 
target_ip=7.1.143.6

I have been able to parse both names and values with

.[].stats.data[] | (.identifier.names[]) and .[].stats.data[] | (.identifier.values[])

but I need help with the jq way to map the values.

Inian
  • 80,270
  • 14
  • 142
  • 161
mr i.o
  • 952
  • 2
  • 10
  • 20

1 Answers1

6

The whole thing can be done in jq using the -r command-line option:

.[].stats.data[]
| [.identifier.names, .identifier.values]
| transpose[]
| "\(.[0])=\(.[1])"
peak
  • 105,803
  • 17
  • 152
  • 177
  • Looks great, can you explain the bit that strips the quotes, please? Assuming I want the quotes, what should I do? This bit "\(.[0])=\(.[1])" – mr i.o Dec 20 '19 at 00:50
  • If you want the outermost quotation marks, omit the -r command-line option. Regarding the last line of the jq program, see "string interpolation" in the jq documentation at https://stedolan.github.io/jq/manual/ . If you want quotation marks elsewhere, note that you can include escaped double-quotes in the string. – peak Dec 20 '19 at 01:09