I have several json files in a folder and subfolders for which I want to print only 3 fields. I use a for loop over all json files, but here to make it simple, the input below represents 3 json files. For each one, I want to print the “Filename” and “Value” only when inside each file appears at least one “Dmo = Path”. When a files doesn´t contain a “Dmo = Path” block, then only print the filename.
{
"Filename": "File_213",
"Date": "2024-4-30",
"Blocks": [
{
"Dmo": "WW",
"Value": "23",
"String": "",
},
{
"Dmo": "Path",
"Value": "/Files/2024/abd",
"String": "",
},
{
"Dmo": "Path",
"Value": "/Files/2024/Ndew",
"String": "",
}
]
}
{
"Filename": "File_4",
"Date": "2024-4-30",
"Blocks": [
{
"Dmo": "WW",
"Value": "45",
"String": "",
}
]
}
{
"Filename": "File_43",
"Date": "2024-4-30",
"Blocks": [
{
"Dmo": "Path",
"Value": "/Files/2023/Roi2",
"String": "",
}
}
]
}
My current code and current output is like below
$ awk '/"Filename":/{fnm=$2}
/Dmo/{dmo=$2}
/Value/ {
val=$2;
if (dmo != "")
print fnm,val
else
print fnm
fnm=""; dmo="";val=""}' input
"File_213", "23",
"/Files/2024/abd",
"/Files/2024/Ndew",
"File_4", "45",
"File_43", "/Files/2023/Roi2",
My expected output is:
File_213, /Files/2024/abd
File_213, /Files/2024/Ndew
File_4
File_43, /Files/2023/Roi2