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.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
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
Hi @pa6 ,
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.
Hi @pa6 ,
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
Hi,
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
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
Views
Likes
Replies