【问题标题】:jq is reordering keys within my JSONjq 正在重新排序我的 JSON 中的键
【发布时间】:2016-12-09 00:40:24
【问题描述】:

出于某种原因,我不知道为什么,当我在 jqplay 上测试以下行的格式时,它看起来与我在命令行上运行时不同。

My original data looks as such:

{"type":"user.list","users":[{"type":"user","id":"57db1b1b9jdjd84f99b977785ba","user_id":"janwjf3sqdditwn7c7","anonymous":false,"email":"test@diversifiedventures.net","phone":null,"name":"Joe Smith","pseudonym":null,"avatar":{"type":"avatar","image_url":null},"app_id":"b5vkddxvop","companies":{"type":"company.list","companies":[]},"location_data":{"type":"location_data","city_name":"Murfreesboro","continent_code":"NA","country_name":"United States","latitude":35.8896,"longitude":-86.3166,"postal_code":"37130","region_name":"Tennessee","timezone":"America/Chicago","country_code":"USA"},"last_request_at":1478624249,"last_seen_ip":"69.139.122.123","created_at":1473977115,"remote_created_at":1466313380,"signed_up_at":1466313380,"updated_at":1480970142,"session_count":1,"social_profiles":{"type":"social_profile.list","social_profiles":[]},"unsubscribed_from_emails":false,"user_agent_data":"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko","tags":{"type":"tag.list","tags":[{"type":"tag","id":"587061","name":"Mixpanel Import - 2016-09-15 21:25:08 UTC"}]},"segments":{"type":"segment.list","segments":[{"type":"segment","id":"581a048c4ad88d6b458198d7"},{"type":"segment","id":"5845cd57c91b1fe3e440032b"},{"type":"segment","id":"5845cab9d951b43e7d33d2f7"}]},"custom_attributes":{"mixpanel_id":"1556710d919339-0beb72f2966b408-69581467-cff8c-1556710d91a54b","claimCount":"2","memberType":"claimant"}}],"scroll_param":"56386a4d-743b-470a-ae9f-848b991e0ccc"}

如果您将其转储到 jq playground, 中,它会准确地格式化它应该如何以及我想要的格式,which looks like this

{"type":"user","id":"57db1b1b9jdjd84f99b977785ba","user_id":"janwjf3sqdditwn7c7","anonymous":false,"email":"test@diversifiedventures.net","phone":null,"name":"Joe Smith","pseudonym":null,"avatar":{"type":"avatar","image_url":null},"app_id":"b5vkddxvop","companies":{"type":"company.list","companies":[]},"location_data":{"type":"location_data","city_name":"Murfreesboro","continent_code":"NA","country_name":"United States","latitude":35.8896,"longitude":-86.3166,"postal_code":"37130","region_name":"Tennessee","timezone":"America/Chicago","country_code":"USA"},"last_request_at":1478624249,"last_seen_ip":"69.139.122.123","created_at":1473977115,"remote_created_at":1466313380,"signed_up_at":1466313380,"updated_at":1480970142,"session_count":1,"social_profiles":{"type":"social_profile.list","social_profiles":[]},"unsubscribed_from_emails":false,"user_agent_data":"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko","tags":{"type":"tag.list","tags":[{"type":"tag","id":"587061","name":"Mixpanel Import - 2016-09-15 21:25:08 UTC"}]},"segments":{"type":"segment.list","segments":[{"type":"segment","id":"581a048c4ad88d6b458198d7"},{"type":"segment","id":"5845cd57c91b1fe3e440032b"},{"type":"segment","id":"5845cab9d951b43e7d33d2f7"}]},"custom_attributes":{"mixpanel_id":"1556710d919339-0beb72f2966b408-69581467-cff8c-1556710d91a54b","claimCount":"2","memberType":"claimant"}}

这正是我希望重组数据的方式。

转到 jqplay 的链接并亲自查看,只需将其输入过滤器并单击压缩输出:

.users[] 

但由于某种原因,当我在命令行中运行它时:

curl https://api.example.com/users/scroll -u 'dG9rOjg4YmUxMzr4XzJmNzZfNDkwY3934jU2X2mrM2MxZGEzYWI2MzoxOjA=': -H 'Accept:application/json'| jq -c '.users[]'

It spits out a result that is totally not formmated how it is suppose to come out, despite using the exact same filter:

{"custom_attributes":{"memberType":"claimant","claimCount":"2","mixpanel_id":"1556710d919339-0beb72f2966b408-69581467-cff8c-1556710d91a54b"},"segments":{"segments":[{"id":"581a048c4ad88d6b458198d7","type":"segment"},{"id":"5845cd57c91b1fe3e440032b","type":"segment"},{"id":"5845cab9d951b43e7d33d2f7","type":"segment"}],"type":"segment.list"},"tags":{"tags":[{"name":"Mixpanel Import - 2016-09-15 21:25:08 UTC","id":"587061","type":"tag"}],"type":"tag.list"},"user_agent_data":"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko","unsubscribed_from_emails":false,"social_profiles":{"social_profiles":[],"type":"social_profile.list"},"session_count":1,"updated_at":1480970142,"signed_up_at":1466313380,"pseudonym":null,"name":"Joe Smith","phone":null,"email":"test@diversifiedventures.net","anonymous":false,"user_id":"janwjf3sqdditwn7c7","id":"57db1b1b9jdjd84f99b977785ba","type":"user","avatar":{"image_url":null,"type":"avatar"},"app_id":"b5vkddxvop","companies":{"companies":[],"type":"company.list"},"location_data":{"country_code":"USA","timezone":"America/Chicago","type":"location_data","city_name":"Murfreesboro","continent_code":"NA","country_name":"United States","latitude":35.8896,"longitude":-86.3166,"postal_code":"37130","region_name":"Tennessee"},"last_request_at":1478624249,"last_seen_ip":"69.139.122.123","created_at":1473977115,"remote_created_at":1466313380}

如您所知,这与 jqplayground 功能中的结果完全不同。更令人困惑的是,其他人运行了它并正确格式化了它。最重要的是,大约一周前,它对我来说效果很好。

我也试过删除 jq 并重新安装它,但没有成功。如果有帮助的话,我会在 Ubuntu 14.04.2 LTS(GNU/Linux 3.13.0-51-generic x86_64)上运行它。

任何帮助将不胜感激!

【问题讨论】:

  • 那么它有什么“问题”?怎么没有按照你的预期格式化?
  • @Jeff - 这是一个按键顺序问题。
  • @wizkids121 - 请注明您使用的jq版本。

标签: json key jq


【解决方案1】:

jq 1.3 并不总是尊重 JSON 对象中键的顺序。假设您使用的是 jq 1.3(或更早版本),解决方案是升级到更高版本(1.4、1.5 或“主”版本)。

【讨论】:

    猜你喜欢
    • 2022-10-17
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2014-09-11
    • 2016-06-03
    • 2016-01-17
    • 2020-03-06
    相关资源
    最近更新 更多