0

I am trying to extract a specific field from some JSON output using jq but am getting some syntax errors. I'm hoping this is just a rookie mistake. Here is the JSON output:

{"data":[{"astronomicalDawn":"2023-03-24T23:42:04+00:00","astronomicalDusk":"2023-03-25T15:10:03+00:00","civilDawn":"2023-03-25T00:48:10+00:00","civilDusk":"2023-03-25T14:03:57+00:00","moonFraction":0.09985874195321992,"moonPhase":{"closest":{"text":"New moon","time":"2023-03-21T11:28:00+00:00","value":0},"current":{"text":"Waxing crescent","time":"2023-03-24T12:00:00+00:00","value":0.10234141908660555}},"moonrise":"2023-03-24T02:35:14+00:00","moonset":"2023-03-24T16:50:28+00:00","nauticalDawn":"2023-03-25T00:15:30+00:00","nauticalDusk":"2023-03-25T14:36:37+00:00","sunrise":"2023-03-25T01:15:56+00:00","sunset":"2023-03-25T13:36:10+00:00","time":"2023-03-24T12:00:00+00:00"}],"meta":{"cost":1,"dailyQuota":10,"lat":41.6483,"lng":70.3366,"requestCount":2,"start":"2023-03-24 12:00"}}

The text I'm trying to extract is the text of the current moon phase, in this case "Waxing Gibbous", however when I try to run it through jq I encounter this error:

pablo@gaviota=> cat stormglass.out | jq '.data.moonPhase.current.text'
jq: error (at <stdin>:1): Cannot index array with string "moonPhase"

I've tried several variations, such as putting "moonPhase" into double quotes, but so far have not stumbled upon the correct solution, so I thought I'd reach out. Thanks in advance!

  • 2
    `.data` is an array so you'll need `.data[].moonPhase.current.text` to get them all, or just `.data[0].moonPhase.current.text` to get the first object's value – 0stone0 Mar 24 '23 at 17:16

0 Answers0