jq: nested object, extract top-level id and lift a value from internal object

后端 未结 2 1770
醉话见心
醉话见心 2020-12-29 23:20

Given the following xample.json;

[
 {
  \"id\": 12345678,
  \"stuff\": { \"book\": \"shelf\", \"hook\": \"line\", \"took\": \"off\", \"info-spec         


        
相关标签:
2条回答
  • 2020-12-29 23:46

    interesting, the issue is indeed the "-" character and this works for me:

    jq '.[] | { id, "info-spec": .stuff."info-spec" }' xample.json
    {
      "id": 12345678,
      "info-spec": 12
    }
    {
      "id": 12345679,
      "info-spec": 23
    }
    

    yet your jq does not seem to like that syntax as it broke on the following and mine does not:

    jq '.[] | .stuff."info-spec"' xample.json
    12
    23
    

    I use:

    jq --version
    jq-1.4
    

    edit: looks like a version issue with <1.4 indeed: https://github.com/stedolan/jq/issues/38

    0 讨论(0)
  • 2020-12-30 00:03

    I managed to figure it out.

    $ jq '.[] | { id, "info-spec": .stuff["info-spec"] }' xample.json
    {
      "info-spec": 12,
      "id": 12345678
    }
    {
      "info-spec": 23,
      "id": 12345679
    }
    

    The key here seems to be to use the newkey: .complex["key"] notation for lifting.

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