Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq substring gives "jq: error: Cannot index string with object"

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]'
like image 212
bcorso Avatar asked Nov 10 '22 07:11

bcorso


1 Answers

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.

like image 144
ryuusenshi Avatar answered Nov 29 '22 09:11

ryuusenshi