Read json file from resources folder in Java | Community
Skip to main content
Level 4
October 10, 2023
Solved

Read json file from resources folder in Java

  • October 10, 2023
  • 3 replies
  • 8019 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mahedi_Sabuj

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-and-map-it-to-the-sling-model/m-p/623533

 

 

3 replies

Hemant_arora
Level 8
October 10, 2023
  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

Level 4
October 10, 2023

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?

 

Mahedi_Sabuj
Community Advisor
Mahedi_SabujCommunity AdvisorAccepted solution
Community Advisor
October 10, 2023

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-and-map-it-to-the-sling-model/m-p/623533

 

 

Mahedi Sabuj
Level 2
October 30, 2023

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 

 

kautuk_sahni
Community Manager
Community Manager
October 11, 2023

@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