Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Read json file from resources folder in Java

Avatar

Level 5

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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"));

[1]: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-read-a-json-file-an...

 

 

View solution in original post

5 Replies

Avatar

Level 9
  1. 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:

  2. <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> <!-- Use the latest version available --> </dependency>

 

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

Avatar

Level 5

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?

goyalkritika_0-1696911579086.png

 

Avatar

Correct answer by
Community Advisor

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"));

[1]: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-read-a-json-file-an...

 

 

Avatar

Level 2

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 

 

Avatar

Administrator

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



Kautuk Sahni