jq正在重新排序我的JSON中的密钥

时间:2016-12-09 00:40:24

标签: json key jq

出于某种原因,我不知道为什么,当我在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的链接,只需在过滤器中输入并点击紧凑输出即可查看yourselft:

.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)上运行它,如果它有所帮助。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

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