Forum Discussion
ZDGN
Contributor
Hi sparrowenclave,
Here is a sample script to browse a directory containing json files and open each to find a specific node.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import static java.util.stream.Collectors.toList;
File newFile;
String FolderPath = "<insert your path here>";
File selectedDirectory = new File(FolderPath);
List<Path> paths = new ArrayList<>();
try {
paths = Files.walk(Paths.get(new File(selectedDirectory.getPath())
.getAbsolutePath()))
.filter(p -> p.toFile().isFile())
.collect(toList());
} catch (IOException e) {
e.printStackTrace();
}
for (Path path : paths) {
newFile = new File(path.toString());
ObjectMapper mapperFileSource = new ObjectMapper();
JsonNode rootNode = null;
try {
rootNode = mapperFileSource.readTree(newFile);
} catch (IOException e) {
e.printStackTrace();
}
JsonNode searchNode = rootNode.findValue("<insert a node name here>");
if (searchNode != null) {
log.info(searchNode.textValue());
// You can add assertions here
}
}
It is just an example of what can be done.
Keep in min you'll need jackson's library.
Hope this will help.
David.
Related Content
- 2 months agoMarius78
Recent Discussions
- 14 hours agoruchisingh