0

I'm strugling to find a solution to this, every other question I've found its quite close but still not the answer.

I've got a huge json, and it has many objects and arrays inside. What I'd like do to is extract the whole object from that json that contain a key named "time" or "time". I've found solutions related, however they just brings the key and not the whole object.

Ex 1:

    {
      "time": -762,
      "type": "chatwheel",
      "key": "156",
      "slot": 1,
      "player_slot": 1
    }

Ex 2:

{
  "time": 888,
  "type": "building_kill",
  "unit": "npc_knight",
  "key": "npc_goodguys_tower1",
  "slot": 7,
  "player_slot": 130
},

Ex 3:

{
  "time": 681,
  "type": "obs_left_log",
  "key": "[132, 124]",
  "slot": 0,
  "x": 132,
  "y": 124,
  "z": 130,
  "entityleft": true,
  "ehandle": 5014545,
  "player_slot": 0
},

Ex 4:

  "purchase_log": [
    {
      "time": -89,
      "key": "orb_of_venom"
    },
    {
      "time": -89,
      "key": "tango"
    },

So, basically, I need to get not only the time key, i need to get the whole object that the key "time" belongs. (like the examples) Is there a way to do it?

Thank you

shiguti
  • 19
  • 4
  • have a look at this way to inspect the hole json stuct: https://stackoverflow.com/questions/10459917/traversing-through-json-string-to-inner-levels-using-recursive-function You then have to remember the parent and to detect the time-key – split Aug 23 '18 at 18:07
  • Please follow the [mcve] guiidelines as much as possible. Also, the distinction between `a key named "time" or "time”` is unclear. – peak Aug 24 '18 at 02:37

1 Answers1

0

If your jq has walk, and if your computer has enough memory:

walk(if type == "object" 
     then if has("time") then . else empty end
     else . end )

To get a copy of the def of walk, see e.g. https://github.com/stedolan/jq/blob/master/src/builtin.jq

peak
  • 105,803
  • 17
  • 152
  • 177