Filter löschen
Filter löschen

Read a userid file and search for userid.json in directory and then search json file

46 Ansichten (letzte 30 Tage)
I know what I need to do, just having a hard to getting it going.
I have this userid.txt file that contains the only user files that I want to consider. If the userid say, 1821407, matches a json file within the directory, for example 1821407.json (all json files have this format), I want to check in the file for some data. I'm thinking I can do the search within the json file part. How do I use Matlab to perform the userid comparison within the directory?

Akzeptierte Antwort

Benjamin Kraus
Benjamin Kraus am 10 Jul. 2024 um 14:08
I suspect you want to use the dir command to get a list of files in the directory, remove ".json" from the end of the file names, then search that list for your target user IDs.
Something like this perhaps:
userIDs = readmatrix('userids.txt');
jsonFiles = dir('*.json');
jsonFileNames = string({jsonFiles.name});
jsonFileUserIDs = extractBefore({jsonFiles.name},".json");
fileExistsForUserID = ismember(string(userIDs), jsonFileUserIDs);
filesWithUserIDInList = ismember(jsonFileUserIDs, string(userIDs));
jsonFilesFromList = jsonFileNames(filesWithUserIDInList);
for f = 1:numel(jsonFilesFromList)
fileName = jsonFilesFromList(f);
data = readstruct(fileName);
% Do whatever you want with the data here.
end
  1 Kommentar
Sunshine
Sunshine am 15 Jul. 2024 um 15:10
Thank you for your insight. I was able to take this code and pull out json info. Your help is very much appreciated.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

VINAYAK LUHA
VINAYAK LUHA am 10 Jul. 2024 um 13:50
Bearbeitet: VINAYAK LUHA am 12 Jul. 2024 um 10:00
Hi Sunshine,
To find the .JSON files with filenames same as the ones in your "userids.txt" file, you may use the MATLAB "textscan" function as follows -
fileID = fopen('userids.txt', 'r');
userIDs = textscan(fileID, '%s');
fclose(fileID);
userIDs = userIDs{1};
% Find uniqueUserIds since some userIds in your list are repeated eg. -226340
uniqueUserIDs = unique(userIDs);
jsonFiles = dir('*.json');
for i = 1:length(uniqueUserIDs)
userID = uniqueUserIDs{i};
jsonFileName = strcat(userID, '.json');
if ismember(jsonFileName, {jsonFiles.name})
fprintf('File %s found.\n', jsonFileName);
else
fprintf('File %s not found.\n', jsonFileName);
end
end
Further, to search within the .JSON files, you can use the MATLAB "fileread" and "jsondecode" functions, For more details refer to the following documentations -
  1. textscan - https://www.mathworks.com/help/matlab/ref/textscan.html
  2. fileread-https://www.mathworks.com/help/matlab/ref/fileread.html
  3. jsondecode-https://www.mathworks.com/help/matlab/ref/jsondecode.html
Hope this helps.
Regards,
Vinayak
  3 Kommentare
VINAYAK LUHA
VINAYAK LUHA am 12 Jul. 2024 um 9:59
Hi Sunshine,
You can replace the "userIDs" array with the "uniqueUserIDs" array within the loop in the code.
I have also updated my code to reflect this change.
Sunshine
Sunshine am 15 Jul. 2024 um 15:09
Thank you. The update does uniquely identify json file names that are within my userid file. I appreciate your assistance.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by