'Read a value out of a "complicated" JSON file [closed]

I'm using jq, but I didn't manage to get a value extracted from a .json-file (I got generated by mediainfo) in my batch-script.

How can I get the value of "Duration" in a variable for further use?

{
"creatingLibrary": {
"name": "MediaInfoLib",
"version": "21.09",
"url": "https://mediaarea.net/MediaInfo"
},
"media": {
"@ref": "J:\\Austria\\Tag5\\EOS_ben\\100EOS5D\\TW_6248.MOV",
"track": [
{
"@type": "General",
"VideoCount": "1",
"AudioCount": "1",
"OtherCount": "1",
"FileExtension": "MOV",
"Format": "MPEG-4",
"Format_Profile": "QuickTime",
"CodecID": "qt  ",
"CodecID_Version": "2007.09",
"CodecID_Compatible": "qt  /CAEP",
"FileSize": "37229924",
"Duration": "9.520",
"OverallBitRate_Mode": "VBR",
"OverallBitRate": "31285650",
"FrameRate": "25.000",
"FrameCount": "238",
"StreamSize": "100484",
"HeaderSize": "98304",
"DataSize": "37130652",
"FooterSize": "968",
"IsStreamable": "Yes",
"Encoded_Date": "UTC 2014-03-13 22:31:17",
"Tagged_Date": "UTC 2014-03-13 22:31:17",
"File_Created_Date": "UTC 2014-03-13 21:31:26.000",
"File_Created_Date_Local": "2014-03-13 23:31:26.000",
"File_Modified_Date": "UTC 2014-03-13 21:31:26.000",
"File_Modified_Date_Local": "2014-03-13 23:31:26.000",
"Copyright": "BEN PAYA",
"extra": {
"com_apple_quicktime_make": "Canon",
"com_apple_quicktime_model": "Canon EOS 5D Mark III",
"com_apple_quicktime_rating_user": "0.000",
"com_apple_quicktime_author": "PhilFried"
}
},
{
"@type": "Video",
"StreamOrder": "0",
"ID": "1",
.
.
.


Solution 1:[1]

That would be

your_json_file.media.track[0].Duration

Where .track[index] denotes a certain element (track), in this case the first one.


You should try an online JSON formatter like, JSON Formatter & Validator, to get automatic indentation.

This is your file content indented:

{
   "creatingLibrary":{
      "name":"MediaInfoLib",
      "version":"21.09",
      "url":"https://mediaarea.net/MediaInfo"
   },
   "media":{
      "@ref":"J:\\Austria\\Tag5\\EOS_ben\\100EOS5D\\TW_6248.MOV",
      "track":[
         {
            "@type":"General",
            "VideoCount":"1",
            "AudioCount":"1",
            "OtherCount":"1",
            "FileExtension":"MOV",
            "Format":"MPEG-4",
            "Format_Profile":"QuickTime",
            "CodecID":"qt  ",
            "CodecID_Version":"2007.09",
            "CodecID_Compatible":"qt  /CAEP",
            "FileSize":"37229924",
            "Duration":"9.520",
            "OverallBitRate_Mode":"VBR",
            "OverallBitRate":"31285650",
            "FrameRate":"25.000",
            "FrameCount":"238",
            "StreamSize":"100484",
            "HeaderSize":"98304",
            "DataSize":"37130652",
            "FooterSize":"968",
            "IsStreamable":"Yes",
            "Encoded_Date":"UTC 2014-03-13 22:31:17",
            "Tagged_Date":"UTC 2014-03-13 22:31:17",
            "File_Created_Date":"UTC 2014-03-13 21:31:26.000",
            "File_Created_Date_Local":"2014-03-13 23:31:26.000",
            "File_Modified_Date":"UTC 2014-03-13 21:31:26.000",
            "File_Modified_Date_Local":"2014-03-13 23:31:26.000",
            "Copyright":"BEN PAYA",
            "extra":{
               "com_apple_quicktime_make":"Canon",
               "com_apple_quicktime_model":"Canon EOS 5D Mark III",
               "com_apple_quicktime_rating_user":"0.000",
               "com_apple_quicktime_author":"PhilFried"
            }
         }
      ]
   }
}

Solution 2:[2]

You may solve your problem in a very simple way via a pure Batch file:

@echo off
setlocal

for /F "tokens=2 delims=:, " %%a in ('findstr "Duration" test.txt') do set "Duration=%%~a"

echo Duration = %Duration%

Furthermore, you can get the values of all fields in the same way:

@echo off
setlocal

for /F "tokens=1,2 delims=:, " %%a in ('findstr /V "{ [ ] }" test.txt') do set "%%~a=%%~b"

rem Here you have the values of *all fields* in the json file. For example:

echo Duration = %Duration%
echo FrameCount = %FrameCount%
echo CodecID_Version = %CodecID_Version%

Output:

Duration = 9.520
FrameCount = 238
CodecID_Version = 2007.09

Solution 3:[3]

To get to FrameCount in the JSON content, the solution for me is:

jq.exe -r .media.track[0].FrameCount C:\MP4convert\test\%filenam%.json > test1.txt
for /f "delims=" %%x in (test1.txt) do set FrCountVar=%%x

For future use, I put the count in a new variable:

SET FrCount=%FrCountVar%

Thanks Finkle MacGraw's answer.

Solution 4:[4]

@ECHO OFF
SETLOCAL

rem The following settings for the source directory & filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q71716898.txt"

REM (
FOR /f "usebackqtokens=1,2delims=:, " %%b IN ("%filename1%") DO (
 IF /i "%%~b"=="duration" SET "duration=%%~c"
)
SET duration
GOTO :EOF

Reasonably standard for /f "tokens processing.

The for/f assigns tokens from each line in turn, token 1 to %%b and token 2 to %%c.

Choosing delimiters of :, , and space, the first token on the target line is "Duration" and the second "9.520".

So, check that "%%~b" (the token, stripped of quotes, then re-enclosed in quotes is the same as the string duration, enclosed in quotes) and if so, assign the value of the second token to the variable duration.

The dequote/enquote ceremony is required in case the token contains separator characters which would interfere with the if parsing, and the /i makes the comparison case-insensitive.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 Aacini
Solution 3 Peter Mortensen
Solution 4 Magoo