Home > Blockchain >  read value out of "complicated" json-file
read value out of "complicated" json-file

Time:04-02


I'm using jq, but didn't manage to get a value extracted from a .json-file (I got generated by mediainfo) in my batch-script.
I'd need the value of "Duration" in a variable for further use...
could someone give me a hint, I'm confused with all this {[ ]} ...
{
"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",
.
.
.

CodePudding user response:

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 a JSON formatter like https://jsonformatter.curiousconcept.com/ to get automatic indentation.

edit: 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"
            }
         }
      ]
   }
}

CodePudding user response:

@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.

  • Related