I have the following JSON data :
$ 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>"
}
I tried this command cat toto.js | jq '[.][-1]'
but it outputs the whole file.
I also tried this command cat toto.js | jq 'last(.)'
but it also outputs the whole file.
I expect only the last element :
{
"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>"
}
2
Answers
If you slurp (
-s
) the file, you can use thelast
keyword:Yes! In a way, the whole point of jq is that it can process streams like yours efficiently, even when those streams are not ndjson.
In your case, just use the -n command-line option and the program:
——
As you noticed, the ostensibly similar program
last(.)
, by contrast, executes iteratively for each JSON input.