-1

This is a follow up question from this question that got contributed by another person. I have following output from a webserver:

{
    "status":"OK",
    "result":{
        "string1":{
            "variable":0
        },
        "string2":[
            {
                "id":"XXXXX:XXXXX",
                "tier":"normal",
                "latitude":"01.XXXXX",
                "longitude":"02.XXXXX"
            },
            {
                "id":"XXXXX:XXXXX",
                "tier":"normal",
                "latitude":"01.XXXXX",
                "longitude":"02.XXXXX"
            },
            {
                "id":"XXXXX:XXXXX",
                "tier":"special",
                "latitude":"01.XXXXX",
                "longitude":"02.XXXXX"
            },
            {
                "id":"XXXXX:XXXXX",
                "tier":"normal",
                "latitude":"01.XXXXX",
                "longitude":"02.XXXXX"
            }
        ]
    }
}

i'm extracting the ids with jq -r '.result.string2[].id' responses.json right now and safing them as a responses.json file; this works perfectly fine.

However, my question is, how do i set the condition so jq will only output me the id's if tier: "special"? I tried to solve it with the answer in this thread, alias these two:

jq -r '.result.string2[] | select(.tier == "special") | .id'responses.json

jq -r '.result.string2[] | select(.tier == "special") | .id' responses.json

This doesn't seem to work in my Windows-PowerShell. It gives me the following error message:

 jq: error: special/0 is not defined at <top-level>, line 1:
.result.string2[] | select(.tier == special) | .id jq: 1 compile error

Thanks for answering!

6
  • @steeldriver The special in the question appears quoted. I don't know Powershell at all. Does it do strange things to quoted strings? Commented Apr 4, 2022 at 22:36
  • I'll eat a little crow here. I installed jq on my windows machine and I get the same error even with valid json. I sort of apologize but not really. You came here and copied invalid input, the wrong command, and the wrong error from another question. And then told us just not to worry about it and to trust you. That is not how it works. Commented Apr 4, 2022 at 22:57
  • @jesse_b So, a Powershell issue then. Commented Apr 4, 2022 at 22:57
  • 1
    @Kusalananda: seems like it. I'm trying to get it to work but I have no idea why it's behaving this way. Commented Apr 4, 2022 at 22:58
  • 4
    I’m voting to close this question because the issue is not related to unix or linux. Commented Apr 4, 2022 at 23:14

1 Answer 1

2

You need to escape the double quotes likely because powershell does not preserve them within single quotes like a POSIX shell does.

jq -r '.result.string2[] | select(.tier == \"special\") | .id'
2
  • 2
    On Windows commands have to parse their own command lines, it's jq taking out the double quotes from its own command line while it tries to interpret it (cf. errors from jq -n '"abc"' and jq -n "'abc'"). Powershell interprets the single quotes and gives jq a single string with the double quotes intact, which is why backslash-escape works rather than Powershell's backtick-escaping - jq understands the backslashes to escape the quotes. Commented Apr 5, 2022 at 0:17
  • PowerShelll does preserve "" inside ''. The OP just used the wrong tool. Native PowerShell ConvertFrom-Json and ConvertTo-Json are much better and are portable to all PowerShell platforms Commented Jun 21, 2023 at 15:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.