Search code examples
windowsjqstartupchocolatey

How to add Walk/1 to jq 1.5 installed by Chocolatey NuGet on Windows 10


I have been automating some data processing within a batch environment using jq. I recently ran into a use case where I need to recursively apply fromjson to strings inside of my json data. Unfortunately jq 1.5 does not have the walk/1 function natively so I need to add it. I am having trouble finding the correct location to add the code. I need to add this code:

def walk(f):
  . as $in
  | if type == "object" then
      reduce keys_unsorted[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
  end;

To my initialization file, but I cannot find it. Does anyone know where the initialization fold is when jq is installed with chocolatey?


Solution

    1. The standard installation does not include a .jq file or .jq directory.

    2. There are some good reasons not to use ~/.jq as a file, but setting those aside for the time being, if you want to use ~/.jq as a startup file, just create it. You might need to check (or alter) the HOME environment variable. That is, jq's idea of where to look depends on HOME. You might find you need to set or reset the environment variable HOME.

    3. The main reason for not using ~/.jq as a startup file is that if ~/.jq is a directory, jq's module system will take note of that. So you may simply want to use the module system, though this has the disadvantage that you'll need to include an include MODULE; or import MODULE as _; directive in your scripts.

    4. You should consider upgrading to jq 1.6. If this is not possible with choco, you can snarf the relevant jq.exe from Appveyor -- see https://github.com/stedolan/jq/wiki/Installation#windows-using-appveyor


    There is in my opinion room for improvement here -- you might like to repurpose your issue at https://github.com/stedolan/jq/issues/1955 with that in mind.