65

I would like to get rid of the timestamp field here using jq JSON processor.

[
  {
    "timestamp": 1448369447295,
    "group": "employees",
    "uid": "elgalu"
  },
  {
    "timestamp": 1448369447296,
    "group": "employees",
    "uid": "mike"
  },
  {
    "timestamp": 1448369786667,
    "group": "services",
    "uid": "pacts"
  }
]

White listing would also works for me, i.e. select uid, group

Ultimately what I would really like is a list with unique values like this:

employees,elgalu
employees,mike
services,pacts
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110

3 Answers3

118

If you just want to delete the timestamps you can use the del() function:

jq 'del(.[].timestamp)' input.json

However to achieve the desired output, I would not use the del() function. Since you know which fields should appear in output, you can simply populate an array with group and id and then use the join() function:

jq -r '.[]|[.group,.uid]|join(",")' input.json

-r stands for raw ouput. jq will not print quotes around the values.

Output:

employees,elgalu
employees,mike
services,pacts
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 4
    Great answer! Furthermore, if you're going to be using this on a CSV, consider using the `@csv` filter instead of `join`ing. –  Nov 24 '15 at 14:12
  • 2
    Relatedly, to select multiple path expressions to delete we can `del(.[]["timestamp", "group"])` – phs Aug 19 '19 at 19:30
  • Why would you not use the delete function? It is far simpler to understand and works on more cases than the second option. What is the bad side of use this option? Because I want to use it since the keys in each object can be variables. – user14492 Jun 30 '21 at 02:50
  • I was thinking it gets verbose if the number of fields to delete is larger than the number of fields to keep. However, you may use what works best for you – hek2mgl Jun 30 '21 at 09:24
8

For the record, an alternative would be:

$ jq -r '.[] | "\(.uid),\(.group)"' input.json

(The white-listing approach makes it easy to rearrange the order, and this variant makes it easy to modify the spacing, etc.)

The following example may be of interest to anyone who wants safe CSV (i.e. even if the values have embedded commas or newline characters):

$ jq -r '.[] | [.uid, .group] | @csv' input.json
"elgalu","employees"
"mike","employees"
"pacts","services"
peak
  • 105,803
  • 17
  • 152
  • 177
-2

Sed is your best friend - I can't think of anything simpler. I've got here having the same problem as the question's author - but maybe this is a simpler answer to the same problem:

< file sed -e '/timestamp/d'
ouflak
  • 2,458
  • 10
  • 44
  • 49
pedr0
  • 11
  • 3