I have a resources folder parallel to my core bundle in project codebase. There is a json file in it. I want to read that json file from my Java class and fetch the value for a particular key from it. How can I do so?
Solved! Go to Solution.
Views
Replies
Total Likes
You can refer to an existing thread [1] to understand how to read a JSON file from the resource folder.
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.JsonNode;
import org.osgi.framework.FrameworkUtil;
// Assuming you have the ObjectMapper instance ready.
ObjectMapper objectMapper = new ObjectMapper();
// Load the JSON file directly into a JsonNode
JsonNode jsonNode = objectMapper.readTree(FrameworkUtil.getBundle(YourModel.class)
.getBundleContext()
.getBundle()
.getResource("lobMap.json"));
Add Jackson Dependency: First, you need to add the Jackson dependency to your project. If you're using Maven, add the following dependency to your pom.xml file:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; public class JsonFileReader { public static void main(String[] args) { // Define the path to your JSON file String jsonFilePath = "path/to/your/file.json"; // Create an ObjectMapper instance (part of Jackson) ObjectMapper objectMapper = new ObjectMapper(); try { // Read the JSON file and parse it into a JsonNode JsonNode rootNode = objectMapper.readTree(new File(jsonFilePath)); // Specify the key you want to fetch String keyToFetch = "your_key_here"; // Fetch the value for the specified key JsonNode valueNode = rootNode.get(keyToFetch); // Check if the key exists in the JSON if (valueNode != null) { // Get the value as a String (you can use other methods like getInt, getBoolean, etc.) String value = valueNode.asText(); System.out.println("Value for key '" + keyToFetch + "': " + value); } else { System.out.println("Key '" + keyToFetch + "' not found in the JSON."); } } catch (IOException e) { e.printStackTrace(); } } }
Replace "path/to/your/file.json" with the actual path to your JSON file, and "your_key_here" with the key you want to fetch
How exactly should I put the path to file if below is the structure of my project and I'm trying to access lobMap.json?
You can refer to an existing thread [1] to understand how to read a JSON file from the resource folder.
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.JsonNode;
import org.osgi.framework.FrameworkUtil;
// Assuming you have the ObjectMapper instance ready.
ObjectMapper objectMapper = new ObjectMapper();
// Load the JSON file directly into a JsonNode
JsonNode jsonNode = objectMapper.readTree(FrameworkUtil.getBundle(YourModel.class)
.getBundleContext()
.getBundle()
.getResource("lobMap.json"));
Hello,
we are using fasterxml , but with the laster 2023.10 sdk for AEM As a Cloud Service, the build fail because of class not found for anything about com.fasterxml.jackson :
some example:
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.util.internal.PrivateMaxEntriesMap$Builder
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMappe
Downgrading to 2023.8 or 2023.9 all build and works correctly .
Thanks,
Francesco
@goyalkritika Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies