5

Problem

I'm trying to filter a json JQ result to only show a substring of the original string. For example if a JQ filter grabed the value

4ffceab674ea8bb5ec421c612536696839bbaccecf64e851dfc270d795ee55d1

I want it to only return the first 10 characters 4ffceab674.


What I've tried

On the Official JQ website you can find an example that should give me what I need:

Command:    jq '.[2:4]'

Input:      "abcdefghi"
Output:     "cd"

I've tried to test this out with a simple example in the unix terminal:

# this works fine, => "abcdefghi"
echo '"abcdefghi"' | jq '.'

# this doesn't work => jq: error: Cannot index string with object
echo '"abcdefghi"' | jq '.[2:4]'
4

2 回答 2

1

So, it turns out most of these filters are not yet in the released version. For reference see issue #289

What you could do is download the latest development version and compile from source. See download page > From source on Linux

After that, if indexing still doesn't work for strings, you should, at least, be able to do explode, index, implode combination, which seems to have been your plan.

于 2014-05-21T04:21:08.420 回答
0

Looking at the jq-1.3 manual I suspect there isn't a solution using that version since it offers no primitives for extacting parts of a string.

于 2017-09-06T23:11:41.367 回答