Extract an information from a JSON file with jq and set the information in a variable

jq create json
jq 'map
jq read from file
jq nested json
jq json to csv
jq add new key/value
jq play
jq extract field

I want to extract an information from a JSON file called blablabla.json which look like this:

  "token": {
   "issued_at": "2018-11-04T23:35:07Z",
   "expires_at": "2018-11-05T00:35:07Z",
   "user": {
    "id": "ide",
    "name": "ide"

I want to get the "expires_at" date to compare it with the current date. To do that, I use this:

type blablabla.json|jq .token".expires_at"

It works, but I don't know how to set it to a variable. I try somethings like:

SET date=type blablabla.json|jq .token".expires_at"

but it does nothing. Do you have an idea?

Thank you very much.

Generally, you'd use a For /F loop:

@Echo Off
For /F "Delims=" %%A In ('type blablabla.json^|jq .token".expires_at"'
) Do Set "Expiry=%%~A"

Where your result can be accessed using the variable, %Expiry%. (You wouldn't set a variable to the name of an important existing system variable).

As for checking it against the current date, you'd need to find a method of doing that as both dates would be strings, not date objects. That is outside of the scope of this question.

Extract an information from a JSON file with jq and set the - html, Extract an information from a JSON file with jq and set the information in a variable - json. Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent

I'd use PowerShell for this (at least as a tool) for several reasons:

  1. it has direct support for json
  2. it has a [datetime] type to do calculations

This one line PowerShell script returns expires.at as a day diffference from today including conversion from UTC.

(([datetime](Get-Content .\blablabla.json |ConvertFrom-Json).token.expires_at).Date - (Get-Date).date).Days

And can easily be wrapped in a batch.

@Echo off
For /f %%A in ('
powershell -NoP -C "(([datetime](Get-Content .\blablabla.json |ConvertFrom-Json).token.expires_at).Date - (Get-Date).date).Days"
') Do Set "Expires=%%A"
Set Expires

This will (for today) return:


and negative values for past dates, positive values for future dates.

JSON on the command line with jq, Suppose that we have a file names.json containing the following json. [{"id": 1, "​name": Because jq is UNIX friendly it is possible to pipe data in and out of it. jq can retrieve values from this document by passing key names. jq works similarly to sed or awk — like a filter that you pipe to and extract values from. Also like sed or awk, it basically has it’s own domain specific language (DSL) for querying JSON. Luckily, it’s really intuitive (unlike awk 😜).

Yes, I know that a .json file should not be processed via a Batch file for a large number of reasons. However, if the problem is simple (like this one) and there are not additional constraints, then a Batch file solution could be the simplest one, like this two-lines code that includes the date comparison...

for /F "tokens=2 delims=, " %%a in ('findstr "expires_at" blablabla.json') do set "expires=%%~a"
if %expires:~0,4%%expires:~5,2%%expires:~8,2% geq %date:~6,4%%date:~0,2%%date:~3,2% goto expired

Reshaping JSON with jq, Extracting user data; Counting Twitter hashtags JSON (JavaScript Object Notation) is a common data sharing format that can describe These set various jq command-line options, or flags, that affect things like input  By default, jq will look at one JSON object at a time when parsing a file; consequently, it can stream very large files without having to load the entire set in to memory. However, in cases where we are aggregating information about the individual objects in a JSON file, we need to give jq access to every JSON object in a file simultaneously.

With Xidel it's as simple as:

xidel -s blablabla.json -e "$json//expires_at"

This returns its value: 2018-11-05T00:35:07Z.

To export this as %expdate%:

FOR /F "delims=" %%A IN ('xidel.exe -s blablabla.json -e "expdate:=$json//expires_at" --output-format=cmd') DO %%A

Xidel can also return the current date in the same format:

xidel -s blablabla.json -e "$json//expires_at,current-dateTime()"

This returns:


How to Parse JSON Files on the Linux Command Line with jq, The "sudo apt-get install jq" command in a terminal window. The simplest way to extract data from a JSON file is to provide a key name to  jq can do a lot but probably the highest frequency use for most users is to pretty print JSON either from a file or after a network call. Suppose that we have a file names.json containing the following json.

How to parse a json file from Linux command line using jq , How to install jq in the most used Linux distributions or compile it from source; How to use jq to parse json-formatted data; How to combine filters  This tutorial shows how to create a simple Workflow in Informatica PowerCenter to extract JSON services and load it into a flat file. Add JSON as an ODBC Data Source. If you have not already, install the driver on the PowerCenter server and client machines. On both machines, specify the connection properties in an ODBC DSN (data source name).

Parsing JSON with jq, jq is a program described as " sed for JSON data": To install jq: From their current legislators file, I've extracted the entry for newly-elected  JSON data are used for various purposes. But JSON data can’t be read easily from JSON file by using bash script like other normal files. jq tool is used to solve this problem. jq command works like sed and awk command, and it uses a domain specific language for working with JSON data.

jq JSON Processor Tutorial: How to access JSON data from the , In this tutorial, you'll learn to use the jq JSON processor utility on the command line. string Duration: 3:42 Posted: Oct 18, 2018 Click the “Download the uncompressed, development jQuery 2.1.4.” Simply, download the latest version if you see a different version than me. Save the file as “jquery.js” in the learnjQuery folder. Step 4) Build a sample data.json. Open Sublime or any text editor and copy the following data into an empty file.