PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

[FIXED] How to nicely remove empty array in jq

 July 26, 2022     jq, json     No comments   

Issue

I am reading O'Reilly's new "Data Science at the Command Line" and running into trouble using jq. I have some JSON (returned from the NYTimes Articles API) I am parsing with jq like so:

jq -c \
'[.response.docs[] | {date: .pub_date, type: .document_type, title: .headline.main }]' \
< myjsonfile.json

So, I am looking for "response":"docs" (which is an array) and then matching every item in that array with "pub_type" etc., renaming it, and so on. This works great, but it adds an empty array at the end:

[{"date":"2009-01-02T00:00:00Z","type":"article","title":"SPARE TIMES: AROUND TOWN"},  
{"date":"2009-01-02T00:00:00Z","type":"article","title":"Catskill Home Prices: How Low Will They Go?"},
{"date":"2009-01-01T00:00:00Z","type":"article","title":"Ominous Cutbacks At Chanel"}]
[] 

How do I get rid of the empty array? My solution right now is to pipe the output back into jq, but that feels really suboptimal. So this works:

jq -c \
'[.response.docs[] | {date: .pub_date, type: .document_type, title: .headline.main }]' | \
< myjsonfile.json | 
jq 'if length > 0 then . else empty end'

But that feels ugly. Is there a nicer way of doing this?


Solution

Use the select filter where length > 0.

select(length > 0)


Answered By - Jeff Mercado
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing