Image of Lucian Ghinda writing for notes.ghinda.com
November 15th, 2024

Script to display VCR body using jq

When I work with VCR in Ruby on Rails, I like to see the structure of the body printed as formatted JSON

The current setup: 

  • have VCR save responses as JSON (it is not always a good idea 😀 )
  • install `jq`
  • setup `~/.local/bin` to be loaded in .zshrc
    How to setup
    How to setup
    Here is the script: 
\#!/usr/bin/env bash

if [ $\# -ne 1 ]; then
    echo "Usage: $0 <json_file>"
    exit 1
fi

input_file="$1"

\# Does the file exists?
if [ ! -f "$input_file" ]; then
    echo "Error: File '$input_file' not found"
    exit 1
fi

\# is jq installed ?
if ! command -v jq &> /dev/null; then
    echo "Error: jq is not installed. Please install it first."
    exit 1
fi

\# Process the JSON file
jq -r '.http_interactions[0].response.body.string' "$input_file" | jq '.'

Here is an example of how to use it: 

Example of output when using the script
Example of output when using the script