Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Dear Team,
We have build a java code based on the requirements of us. When we use in the tjava component it's not working.
import java.io.FileInputStream;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class JSONReadExample
{
public static void main(String[] args) throws Exception
{
String path = "D:\\PIVOT\\source code\\testj2\\uoa_food\\src\\JSONExample.json";
InputStream fileStream = new FileInputStream(path);
JSONTokener jtoken = new JSONTokener(fileStream);
JSONArray ja = new JSONArray(jtoken);
JSONObject jo = ja.getJSONObject(0);
String[] keys = JSONObject.getNames(jo);
String id= jo.get("Id").toString();
String timestamp = jo.get("TimeStamp").toString();
JSONArray newJa = new JSONArray();
for (String key : keys)
{
if(key.equals("Id") || key.equals("TimeStamp"))
{
continue;
}
JSONObject newJo = new JSONObject();
newJo.put("Id", id);
newJo.put("TimeStamp", timestamp);
newJo.put("pi_tag_id", key);
newJo.put("value", jo.get(key).toString());
newJa.put(newJo);
}
String jsonString = newJa.toString(2);
System.out.println(jsonString);
String pathbecome = "D:\\PIVOT\\source code\\testj2\\uoa_food\\src\\JSONExample-result.json";
Files.write(Paths.get(pathbecome), jsonString.getBytes());
}
}
can anyone help to arrange and share based on tjava component style ?
Thanks,
Prabuj
Hi,
Did you add all necessary libraries for the code to execute? tJava component is ideally used to process small snippets of java code. The code like in your post is bit lengthy to manage from tJava. I would recommend to use Talend routines to process the functionality and call the functions inside routine from Talend tJava component.
An example for this method of using routine within a Talend job can be referred from below KB article.
Warm Regards,
Nikhil Thampi
Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved