How convert a JSON object into DAM asset in JAVA | Community
Skip to main content
June 16, 2022
Solved

How convert a JSON object into DAM asset in JAVA

  • June 16, 2022
  • 4 replies
  • 2640 views

Hi,

 

I want to convert a JSON object into a asset and store it in AEM DAM(i.e, store as json file in DAM). I'm aware that we must use AssetManager API for this. But, can someone help me with sample code to achieve this usecase in JAVA.

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 ShaileshBassi

Hi,

 

  • I have a file called sample.json on my local drive
  • I open my AEM 6.5.12 instance and browse into assets, /content/dam/we-retail
  • I drag and drop the sample.json into the browser window, triggering the upload it
  • sample.json is now being displayed as asset
  • I hit localhost:4502/content/dam/we-retail/sample.json
  • It is downloading the JSON file and it can be displayed correctly.

If you are looking for creating the Dam asset from the java code then you can first convert the json object into the input stream using the code as below:

String str = json.getJSONObject("data").toString();
InputStream is = new ByteArrayInputStream(str.getBytes());

and if you already hava a file you can use  as below:

InputStream fis = new FileInputStream(savedFile);

And once you get the input stream you can process it to AEM via the following code:

AssetManager am = resolver.adaptTo(AssetManager.class);
Asset asset = am.createAsset(assetsPath, is, mimeType, true);

In the above using the mimeType as below:

 

Thanks

4 replies

sunil_kumar_
Level 5
June 16, 2022

Hi @parthiban_selvam ,
Can you add some more information. As per my understanding, You want JSONObject to convert to json file and store in dam ?
As you are trying to store JSON object into json file. You must be using this file to store non digital data. you can't use this as digital asset(Image). So what is purpose of creating Asset as you don't have any binary data. You can store this as file(nt:file) instead asset(dam:Asset). This can be use to store and get non digital data and it won't be having renditions. 

 

SantoshSai
Community Advisor
Community Advisor
June 16, 2022

Hi @parthiban_selvam ,

Considering an example below are your Objects

UserEntity user = new UserEntity();
user.setUserName("UserName");
user.setUserAge(18);

Converting them to JSON (There are multiple ways- Gson library, Jackson etc.) the way I'm using is Gson

If the Json data is huge then There are many libraries for serializing/deserializing JSON in java, the most notable is Google’s Gson: https://github.com/google/gson

Gson gson = new Gson();
String jsonStr = gson.toJson(user);

Writing/Uploading to DAM (.json)

ResourceResolver resolver = null;
InputStream is = new ByteArrayInputStream(jsonStr.getBytes()); //we are sending the JSON data as a String.
com.day.cq.dam.api.AssetManager assetMgr = resolver.adaptTo(com.day.cq.dam.api.AssetManager.class);
assetMgr.createAsset(“/content/dam/ab/holidayApi.json”, is, “application/json”, true);
try {
    json = new JSONObject(sb.toString());
   } catch (JSONException e) {
      //
  }
 }
  response.getWriter().println(json);
}

This above piece of code tends to understand the logic. However you will have multiple options to implement them either by servlet, service/component based on your flow of application execution.

Hope that helps!

Regards,

Santosh

Santosh Sai
ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
June 17, 2022

Hi,

 

  • I have a file called sample.json on my local drive
  • I open my AEM 6.5.12 instance and browse into assets, /content/dam/we-retail
  • I drag and drop the sample.json into the browser window, triggering the upload it
  • sample.json is now being displayed as asset
  • I hit localhost:4502/content/dam/we-retail/sample.json
  • It is downloading the JSON file and it can be displayed correctly.

If you are looking for creating the Dam asset from the java code then you can first convert the json object into the input stream using the code as below:

String str = json.getJSONObject("data").toString();
InputStream is = new ByteArrayInputStream(str.getBytes());

and if you already hava a file you can use  as below:

InputStream fis = new FileInputStream(savedFile);

And once you get the input stream you can process it to AEM via the following code:

AssetManager am = resolver.adaptTo(AssetManager.class);
Asset asset = am.createAsset(assetsPath, is, mimeType, true);

In the above using the mimeType as below:

 

Thanks

arunpatidar
Community Advisor
Community Advisor
June 17, 2022

Hi,

This is a sample code of uploading asset from local drive

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/UploadAssets.java 

 

to convert json to java use Gson java api

Arun Patidar