Create a new json string from jq output elements

前端 未结 1 1652
甜味超标
甜味超标 2021-01-25 06:28

My jq command returns objects in brackets but without comma separators. But I would like to create a new json string from it.

This call finds all elements of arr

1条回答
  •  迷失自我
    2021-01-25 07:09

    You can always embed a stream of (zero or more) JSON entities within some other JSON structure by decorating the stream, that is, in the present case, by wrapping the STREAM as follows:

    { arr: [ STREAM ] }
    

    In the present case, however, we can also take the view that we are simply editing the original document, and accordingly use a variation of the map(select(...)) idiom:

    .arr |= map( select(index("FooItem")) | .[3].texts) 
    

    This latter approach ensures that the context of the "arr" key is preserved.

    Addendum

    To filter out the empty strings, simply add another map(select(...)):

    .arr |= map( select(index("FooItem"))
                 | .[3].texts | map(select(length>0)))
    

    0 讨论(0)
提交回复
热议问题