How to convert TSV data to JSON Object in java - java

TSV Data :
Required OutPut :
[{
candidates: {
id:"agent_4",
text = "can i get a confirmation code",
count = 2},
{
id:"agent_11",
text = "text_2",
count =3},
}]
I got the similar question for JQ, but how can I achieve this in java? Convert TSV file to multiple JSON arrays with jq

Load file by BufferedReader and use JsonArray to format your Json output
BufferedReader reader = new BufferedReader(new FileReader("data.tsv"));
String[] fieldNames = reader.readLine().split("\t");
List<JSONObject> rows = new ArrayList<>();
// Read tsv file line by line
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split("\t");
JSONObject row = new JSONObject();
for (int i = 0; i < fieldNames.length; i++) {
row.put(fieldNames[i], fields[i]);
}
rows.add(row);
}
reader.close();
// Convert the list of rows to a JSON array
JSONArray array = new JSONArray(rows);

Related

How to convert InputStream to JsonArray Object using Java

I'm trying to convert InputStream to JSON Array object but not getting the JSON object properly, please find my inputStream record below:
{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"}
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}
I'm getting extra commas for each item and for last record as well - please find the java code below. Can someone please help me to resolve this issue. Appreciated your help in advance. Thanks!
Product.java
public void getProduct() {
String bucketName = "myProductBucket";
String key = "products/product-file";
StringBuilder sb = null;
JSONArray jsonArray = new JSONArray();
try(InputStream inputStream = s3Service.getObjectFromS3(bucketName, key);) {
sb = new StringBuilder();
sb.append("[");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null) {
sb.append(line).append(",");
}
sb.append("]");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
}
Output:
[{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},,
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"},]
Expected Output:
[{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}]
AFAIU, this is expected, since Your JSON object is only partially valid.
Although it is not a valid JSON array either, it could be parsed into JSONArray after small modifications (mind the starting and closing brackets and a comma between the objects):
[
{"id":4,"productId":9949940,"data":"product data 1","productPrice":"653.90"},
{"id":5,"productId":4940404,"data":"product data 2","productPrice":"94.12"}
]
Or, alternatively, You could split the input into individual JSON objects by hand and parse them one by one.
As per your Product details file, You are not having valid JSON array objects in file. So, It can not be possible to directly create JSONArray from the file.
What you can do is, Read the product lines one by one and Create JSONObject and convert it to the JSONArray. Please find below piece of code which can help.
Scanner s = new Scanner(new File("filepath")); //Read the file
JSONArray jsonArray = new JSONArray();
while (s.hasNext()){
JSONObject jsonObject = new JSONObject(s.next());
jsonArray.put(jsonObject);
}
//jsonArray can be print by iterating through it.
Here is the code that you can use, the idea is InputStream does not represent a valid JSON so you have to convert it into a valid JSON string using StringBuilder. But first, you need to take care of the JSON which is not valid.
StringBuilder sb;
try(InputStream inputStream = new FileInputStream(new File("Path"))) {
sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray(sb.toString());
Dependecy
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
</dependency>
Get Stream from file content to List<String>.
FileReader fileReader = null;
try {
File file = new File("path");
InputStream inputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
List<String> list = bufferedReader.lines().collect(Collectors.toList());
JSONArray array = new JSONArray();
for (String s : list) {
array.put(new JSONObject(s));
}
bufferedReader.close();
System.out.println(array);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Objects.nonNull(null)) {
fileReader.close();
}
}
Dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>

Count the number of elements in a Json file

All type of json elements like objects in json files arrays and simple key value pairs.
When you call JSONParser parser = new JSONParser(); obj = parser.parse(path);
parser.parse() expects an actual JSON string, not the path to the JSON file.
It wants to see something like this:
json.parse("{ "name":"John", "age":31, "city":"New York" }");
To fix your code, you can do this:
List < String > list = new ArrayList < >();
try (BufferedReader br = Files.newBufferedReader(Paths.get(path))) {
//br returns as stream and convert it into a List
list = br.lines().collect(Collectors.toList());
} catch(IOException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
for (String s: list) {
sb.append(s);
}
String json = sb.toString()
JSONParser parser = new JSONParser();
obj = parser.parse(json);

Java Read a html file and save its content to a excel file

Html file code sample :
<HTML>
<HEAD>
<TITLE>REPORT</TITLE></HEAD>
<BODY>
<TITLE>REPORT</TITLE><PRE><H2>################ REPORT ###################</H2><H3>Setup</H3> Item1 1120 <br> Item2 Copy free <br> Item3 8/3/2017 5:44:51 AM <br> Item4 <Press OK> <br>
The information I need to read are the lines with <br>. The goal is to save these information to a excel file like below
I currently use BufferedReader to read the html file, but I dont know how to separate the line contains the field and value. I was trying to use hashmap to save its field name and value, but I cant get the value in a correct way. I also tried Jsoup to get rid of the HTML tag, but it gives me more complexity to read the line since the html file
private final String[] modStrings = new String[]{"Item1", "Item2", "Item3", "Item4", "Item5"};
public void readHtmlFile() throws IOException {
FileReader reader = new FileReader("C:\\Users\\file.html");
// StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(reader);
String line;
String[] tempContent = {};
ArrayList content = new ArrayList();
HashMap modMap = new HashMap<>();
while ( (line=br.readLine()) != null) {
tempContent = line.split("<br>");
for(int i = 0; i < tempContent.length; i++){
for (String sub:modStrings){
if(tempContent[i].contains(sub)){
String value = "TODO HERE"; // TODO
content.add(sub);
modMap.put(sub, value);
}
}
}
}
// String textOnly = Jsoup.parse(sb.toString()).text();
for(int i = 0; i < content.size(); i++){
System.out.println(content.get(i));
System.out.println(modMap);
}
}
Any suggestions or ideas will be a lot of help.
The solution for you is simple, just using the util function of String class, based on your html content to use the suitable method to get the content you want. For example here I am using split(String regex), [split(String regex, int limit)](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)),trimorsubString`... to do a simple trick
Sample code for you:
public static void main(String[] args) throws IOException {
String[] modStrings = new String[] { "Item1", "Item2", "Item3", "Item4", "Item5" };
FileReader reader = new FileReader("html.html");
BufferedReader br = new BufferedReader(reader);
String line;
String[] tempContent = {};
ArrayList content = new ArrayList();
HashMap<String, String> modMap = new HashMap<>();
while ((line = br.readLine()) != null) {
if (line.contains("<br>")) {
line = line.substring(line.indexOf("Item1"));
tempContent = line.split("<br>");
for (String item : tempContent) {
if (item.contains("Item")) {
String[] itemArr = item.trim().split(" ", 2);
String itemName = itemArr[0].trim();
String value = itemArr[1].trim();
modMap.put(itemName, value);
}
}
}
}
for(String key : modMap.keySet()){
System.out.println(key + ":" + modMap.get(key));
}
}

Parsing String and Creating a jsonFormat.txt file with a json format inside

I want to create a json format text. Here is my string..
String displayContents = "|Fname:Jose Marie|Mname: Baguio|Lname: Chan|";
And I want this kind of output. Say jsonFormat.txt contains
{"Fname": "Jose Marie", "Mname": "Baguio", "Lname": "Chan"}
Here is my code so far.
String data = (String) displayContents;
StringBuilder buff = new StringBuilder();
String delim = "|";
String[] tokens = data.split(delim);
File myFile = new File("/sdcard/jsonFormat.txt");
buff.append(tokens);
data = data.substring(1, data.length()-1);
FileOutputStream fOuts = new FileOutputStream(myFile);
JSONObject jsonObject = new JSONObject();
for(int index=0;index < tokens.length ;index++){
String[] sub_tokens = tokens[index].split(":");
jsonObject.put(sub_tokens[0],sub_tokens[1]);
}
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOuts);
myOutWriter.append(jsonObject.toString());
myOutWriter.close();
Here's the code so far.
Here:
String[] tokens = data.split(delim);
File myFile = new File("/sdcard/jsonFormat.txt");
buff.append(tokens);
this will save comma separated String in text file as:
{,Fname:Jose Marie,Mname: Baguio,Lname: Chan,}
Create a valid JSON string as:
1. Remove | from start and end :
data =data.substring(1, data.length()-1);
2. spilt String using |:
String[] tokens = data.split(delim);
3. Again spilt String using: delim and create a JSONObject:
JSONObject jsonObject=new JSONObject();
for(int index=0;index<tokens.length();index++){
String[] sub_tokens = tokens[index].split(":");
jsonObject.put(sub_tokens[0],sub_tokens[1]);
}
4. Now save jsonObject in jsonFormat.txt :
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOuts);
myOutWriter.append(jsonObject);
myOutWriter.close();
Finally string stored in text file as:
{"Fname": "Jose Marie", "Mname": "Baguio", "Lname": "Chan"}
Which is valid JSON String.
Instead of using String.spilt we can also create regex for extracting data from provided String

How to read a String (file) to array in java

Suppose there is a file named as SUN.txt
File contains : a,b,dd,ss,
I want to make dynamic array depending upon the number of attributes in file.
If ther is a char after comma then array will be of 0-4 i.e of length 5.
In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.
How do i do that?
Sundhas
You should think about
Reading the file into a String
Splitting the file by separator ','
Using a list for adding the characters and convert the list to an array, when the list is filled
As Markus said, you want to do something like this..
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//The StringBuffer will be used to create a string if your file has multiple lines
StringBuffer sb = new StringBuffer();
String line;
while((line = reader.readLine())!= null)
{
sb.append(line);
}
//We now split the line on the "," to get a string array of the values
String [] store = sb.toString().split(",");
I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.
If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.
If you would like a NULL you would have to add it in yourself at the end so you could do something like
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//Use an arraylist to store the values including nulls
ArrayList<String> store = new ArrayList<String>();
String line;
while((line = reader.readLine())!= null)
{
String [] splitLine = line.split(",");
for(String x : splitLine)
{
store.add(line);
}
//This tests to see if the last character of the line is , and will add a null into the array list
if(line.endsWith(","))
store.add(null);
}
String [] storeWithNull = store.toArray();
Well if you want want to simply open the file and store the content in a array of string then
1) open the file into a string
2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
but I'm curious why you can't use a String file directly ?
For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
data.add(line.split(","));
line = readNextLine();
}
(assuming, your file contains 1..n lines of comma separated values)
You may want to have it like this:
"a,b,c,d," -> {"a", "b", "c", "d", null}
Here's a suggestion how to solve that problem:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
String[] values = new String[5];
String[] pieces = line.split(",");
for (int i = 0; i<pieces.length; i++)
values[i] = pieces[i];
data.add(values);
line = readNextLine();
}
its seems like a CSV file something like this will work assuming it has 5 lines and 5 values
String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
while((line = br.readLine()) != null ){
StringTokenizer s = new StringTokenizer(line,",");
while (s.hasMoreTokens()){
value[row][col] = s.nextToken();
col++;
}
col = 0;
row++;
}
i havent tested this code
Read the file, using BufferedReader, one line at the time.
Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array.
Load the String[] parts into a List.

Categories