Benny
Benny

Reputation: 262

Cannot select fields beginning with numerics in jq (with references to other examples)

I have seen proposed solutions that worked for others in the following pages but I can't get it to work for me using the jqplay browser shell:

https://github.com/stedolan/jq/issues/344

https://github.com/stedolan/jq/issues/345

https://github.com/stedolan/jq/issues/1304

Given this data:

{
    "api_version": 4,
    "error": null,
    "result": [
        {
            "labelId": "ALL",
            "labelName": "ALL",
            "samples": 30104,
            "avgResponseTime": 6.849,
            "90line": 8,
            "95line": 9,
            "99line": 36,
            "minResponseTime": 2,
            "maxResponseTime": 1951,
            "avgLatency": 5.287,
            "geoMeanResponseTime": 5.484,
            "stDev": 23.765,
            "duration": 302,
            "avgBytes": 110.224,
            "avgThroughput": 99.682,
            "medianResponseTime": 5,
            "errorsCount": 0,
            "errorsRate": 0,
            "hasLabelPassedThresholds": null
        },
        {
            "labelId": "3687c89fac2385d28d53b356d4785418",
            "labelName": "100b 3600s Cache",
            "samples": 7300,
            "avgResponseTime": 6.028,
            "90line": 7,
            "95line": 8,
            "99line": 11,
            "minResponseTime": 2,
            "maxResponseTime": 680,
            "avgLatency": 6.021,
            "geoMeanResponseTime": 5.203,
            "stDev": 16.233,
            "duration": 300,
            "avgBytes": 16.581,
            "avgThroughput": 24.333,
            "medianResponseTime": 5,
            "errorsCount": 0,
            "errorsRate": 0,
            "hasLabelPassedThresholds": null
        },
        {
            "labelId": "f88f8ff81bf9b521134637639a0277be",
            "labelName": "100b NonCache",
            "samples": 729,
            "avgResponseTime": 6.143,
            "90line": 7,
            "95line": 7,
            "99line": 9,
            "minResponseTime": 3,
            "maxResponseTime": 877,
            "avgLatency": 6.136,
            "geoMeanResponseTime": 4.627,
            "stDev": 32.817,
            "duration": 295,
            "avgBytes": 1.64,
            "avgThroughput": 2.471,
            "medianResponseTime": 4,
            "errorsCount": 0,
            "errorsRate": 0,
            "hasLabelPassedThresholds": null
        }
    ]
}

I originally attempted:

[.result[] | {labelName: .labelName, samples: .samples, avgResponseTime: .avgResponseTime, 90line: .90line, 95line: .95line, 99line: .99line, minResponseTime: .minResponseTime, maxResponseTime: .maxResponseTime, avgLatency: .avgLatency, geoMeanResponseTime: .geoMeanResponseTime, stDev: .stDev, durationSeconds: .durationSeconds, avgBytes: .avgBytes, avgThroughput: .avgThroughput, medianResponseTime: .medianResponseTime, errorCount: .errorsCount, errorRate: .errorsRate, hasLabelPassedThresholds: .hasLabelPassedThresholds}]

and got

jq: error: syntax error, unexpected LITERAL (Unix shell quoting issues?) at <top-level>, line 1:
[.result[] | {labelName: .labelName, samples: .samples, avgResponseTime: .avgResponseTime, 90line: .90line, 95line: .95line, 99line: .99line, minResponseTime: .minResponseTime, maxResponseTime: .maxResponseTime, avgLatency: .avgLatency, geoMeanResponseTime: .geoMeanResponseTime, stDev: .stDev, durationSeconds: .durationSeconds, avgBytes: .avgBytes, avgThroughput: .avgThroughput, medianResponseTime: .medianResponseTime, errorCount: .errorsCount, errorRate: .errorsRate, hasLabelPassedThresholds: .hasLabelPassedThresholds}]                                                                                           
jq: 1 compile error
exit status 3

Looking at the similar questions asked in the prior links, I attempted to fix with queries like this:

[.result[] | {labelName: .labelName, samples: .samples, avgResponseTime: .avgResponseTime, 90line: .[“90line”], 95line: .[“95line”], 99line: .[“99line”], minResponseTime: .minResponseTime, maxResponseTime: .maxResponseTime, avgLatency: .avgLatency, geoMeanResponseTime: .geoMeanResponseTime, stDev: .stDev, durationSeconds: .durationSeconds, avgBytes: .avgBytes, avgThroughput: .avgThroughput, medianResponseTime: .medianResponseTime, errorCount: .errorsCount, errorRate: .errorsRate, hasLabelPassedThresholds: .hasLabelPassedThresholds}]

to deal with the fields beginning with numeric characters. I still get the same syntax errors, however, so I'm unable to line up my expectations with what I'm seeing in those other solutions. I just can't figure out what I'm doing wrong, I've tried all kinds of other quoting or trying stuff like | tostring to no avail.

EDIT: in response to a proposed answer below:

Hmm, I just can't get it. To recap:

{
    "api_version": 4,
    "error": null,
    "result": [
        {
            "labelId": "ALL",
            "labelName": "ALL",
            "samples": 30104,
            "avgResponseTime": 6.849,
            "90line": 8,
            "95line": 9,
            "99line": 36,
            "minResponseTime": 2,
            "maxResponseTime": 1951,
            "avgLatency": 5.287,
            "geoMeanResponseTime": 5.484,
            "stDev": 23.765,
            "duration": 302,
            "avgBytes": 110.224,
            "avgThroughput": 99.682,
            "medianResponseTime": 5,
            "errorsCount": 0,
            "errorsRate": 0,
            "hasLabelPassedThresholds": null
        }
    ]
}

jq bit:

jq '[.result[] | {labelName: .labelName, samples: .samples, avgResponseTime: .avgResponseTime, minResponseTime: .minResponseTime, maxResponseTime: .maxResponseTime, avgLatency: .avgLatency, geoMeanResponseTime: .geoMeanResponseTime, stDev: .stDev, durationSeconds: .durationSeconds, avgBytes: .avgBytes, avgThroughput: .avgThroughput, medianResponseTime: .medianResponseTime, errorCount: .errorsCount, errorRate: .errorsRate, hasLabelPassedThresholds: .hasLabelPassedThresholds, “90line”: .[“90line”], “95line”: .[“95line”], “99line”: .[“99line”]}]'

Returns:

jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at , line 1:

Upvotes: 3

Views: 959

Answers (2)

Dr-Bracket
Dr-Bracket

Reputation: 5504

If your JSON looks like this (in a file named your.json):

{
  "banana": {
    "9zz": true
  }
}

And you want to grab 9zz with jq, do

jq '.banana["9zz"]' your.json

In other words, the two following lines are identical, but the bottom one works with values beginning with a number:

jq '.one.two' your.json
jq '.one["two"]' your.json

Upvotes: 2

peak
peak

Reputation: 116750

You need to quote the key names of keys that begin with a numeral, e.g.

"90line": .["90line"]

Note also that the jq expression {"90line": .["90line"]} can be abbreviated to just {"90line"}.

Example

With your input:

$ jq '[.result[] | {labelName, "90line": .["90line"] } ]' input.json
[
  {
    "labelName": "ALL",
    "90line": 8
  },
  {
    "labelName": "100b 3600s Cache",
    "90line": 7
  },
  {
    "labelName": "100b NonCache",
    "90line": 7
  }
]

Upvotes: 1

Related Questions