JSON on the command line with jq in Jenkins

7,784

To answer your question directly, no, you can't shell out to jq if it is not installed.

However, depending on exactly what you are doing, you don't even need jq at all.

If you are running a Jenkins Pipeline job:

def version = readJSON(file: 'package.json').version

If you are running a Groovy script using the Groovy Script web UI or using the Groovy-based cli.jar (I haven't tested this, so it probably needs some debugging):

import groovy.json.*

def jsonSlurper = new JsonSlurper()
def reader = new BufferedReader(new InputStreamReader(new FileInputStream('package.json'), 'UTF-8'))
def package = jsonSlurper.parse(reader)  

def version = package.version
Share:
7,784

Related videos on Youtube

Janith
Author by

Janith

Updated on September 18, 2022

Comments

  • Janith
    Janith over 1 year

    I used jq -r '.version' package.json command to filter the version from package.json file using the terminal.

    Can we use jq command in Jenkins shell without locally installing it?

    • ANIL
      ANIL over 5 years
      I don't think it is possible to use jq without installing it. if you don't want to install any additional tools to manipulate your JSON then you can use existing tools that are most likely installed in your system. You can use Python for the task.
    • VocalFan
      VocalFan over 5 years
      Could you expand on what you mean by "without locally installing"? jq's download page mentions that it has "no runtime dependencies", so it could be run as a standalone executable.