尝试从 JSON 列表中获取最后一个对象

问题描述 投票:0回答:1

我有以下 JSON 数据:

$ cat toto.json
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6fe7cfd0d0 @scope=#<SessionsController:0x00000000186758>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ffb16f3f8 @scope=#<SessionsController:0x00000000186910>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6fe7e3e930 @scope=#<SessionsController:0x00000000186870>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ff9a45a10 @scope=#<SessionsController:0x000000001869b0>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ffd3dbc48 @scope=#<SessionsController:0x000000001867f8>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ff75fb880 @scope=#<SessionsController:0x00000000186a50>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ff75ffe80 @scope=#<SessionsController:0x00000000186a28>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ffb34a858 @scope=#<SessionsController:0x00000000188530>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ff724b460 @scope=#<SessionsController:0x00000000188670>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ff6b526e0 @scope=#<SessionsController:0x000000001886e8>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6fdda80500 @scope=#<SessionsController:0x00000000188f08>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6feb2ab010 @scope=#<SessionsController:0x00000000188850>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6fddc9b330 @scope=#<SessionsController:0x00000000188fa8>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ff6740098 @scope=#<SessionsController:0x00000000188b70>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6ffb73f3f0 @scope=#<SessionsController:0x00000000189138>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x00007d6fe8b98d88 @scope=#<SessionsController:0x00000000188990>, @routes=#<ActionDispatch::Routing::RouteSet:0x00007d701b857b28>, @helpers=#<Module:0x00007d701b6f38b8>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x000076156685d378 @scope=#<SessionsController:0x00000000186758>, @routes=#<ActionDispatch::Routing::RouteSet:0x000076159a35afe0>, @helpers=#<Module:0x000076159a1e8b30>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x0000761566955ca8 @scope=#<SessionsController:0x000000001867f8>, @routes=#<ActionDispatch::Routing::RouteSet:0x000076159a35afe0>, @helpers=#<Module:0x000076159a1e8b30>, @script_namer=nil>"
}
{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x000076155c824410 @scope=#<SessionsController:0x00000000188dc8>, @routes=#<ActionDispatch::Routing::RouteSet:0x000076159a35afe0>, @helpers=#<Module:0x000076159a1e8b30>, @script_namer=nil>"
}

我尝试了这个命令

cat toto.js | jq '[.][-1]'
,但它输出了整个文件。

我也尝试过这个命令

cat toto.js | jq 'last(.)'
,但它也输出整个文件。

我只希望最后一个元素:

{
  "method": "GET",
  "path": "/users/sign_in",
  "status": 500,
  "exception.class": "NoMethodError",
  "exception.message": "undefined method `user_azure_activedirectory_v2_omniauth_authorize_path' for #<ActionDispatch::Routing::RoutesProxy:0x000076155c824410 @scope=#<SessionsController:0x00000000188dc8>, @routes=#<ActionDispatch::Routing::RouteSet:0x000076159a35afe0>, @helpers=#<Module:0x000076159a1e8b30>, @script_namer=nil>"
}
json indexing jq
1个回答
0
投票

如果您吞食 (

-s
) 文件,您可以使用
last
关键字:

jq -s 'last' todo.json
© www.soinside.com 2019 - 2024. All rights reserved.