I have below situation. It needs to be implemented in Java.
Take input from a text file, convert the content into a byte array.
Use the above byte array as a part of a JSON object , create a .json file
For point1, i have done something like this.
InputStream is = new ClassPathresource("file.txt").getInputStream();
byte[] ip = IOUtils.toByteArray(is);
For point2, my Json file (containing json object), should look like below.
{
"name": "xyz",
"address: "address here",
"ipdata": ""
}
The ipdata should contain the byte array created in step 1.
How can i create a json object with the byte array created in step 1 as a part of it ? And then write the entire content to a separate .json file ?
Also is the byte array conversion done in step1 an optimum way, or do we need to use any other API(may be to take care of encoding)?Please suggest.
Any help is appreciated. Thanks in advance.
You can simply convert the byte array ip using ip.toString()
Or if you know the encoding you can use ipString = new String(ip, "UTF8")
And then take that string to add to your json object.
Since you are reading a JSON string from file and want to write it back to a new json file you dont need the JSON Object conversion in-between. Just convert the byte[] to String as
String ips = new String(ip);
Now create a JSON Object with the data you want to write to the new file. And then you can write the data to file using FileWriter. PFB the code-
JSONObject obj = new JSONObject();
obj.put("name", "xyz");
obj.put("address", "address here");
obj.put("ipdata", ips);
try(FileWriter fileWriter =
new FileWriter("newFileName.json") ){
fileWriter.write(obj.toString());
}
Related
EDIT:
I am not sure how much the WHY matters here. There are lot of reasons I am doing what I am doing in a certain way not feasible to explain in one post.
But I will still give some context, the service that stores my json files gives them back as byte arrays, so i start with byte[] arrays, each byte array is a json structure.
From this json i need to convert to a matching JAVA POJO this is POJO serves as a validator and then the final step is using Freemarker generating HTML files out of this POJO.
My scenario is that i get potentially get multiple byte arrays. Each can be a maximum of 500mb. I need to combine them, convert to JSON string and then convert to JAVA POJO.
There are couple of problems i am trying to address:
I see the jsonData only having 1000 records in all.
How do I combine the two jsons into one single json to be then able to convert to JAVA POJO?
Here's my JSON structure:
{
"Header": {
"id": 123
},
"Data": [
{
"key": "123
}
]
}
Here's my current code stripped to bare which I use for BYTE[] -> JSON STRING - > POJO
byte[] dataFile = null;
for (String fileName : getFileNames()) {
try {
byte[] currentFile = null;
currentFile = SomeService.getByteFile(fileName);
dataFile = ArrayUtils.addAll(dataFile, currentFile);
}
String jsonData = new JSONObject(new String(dataFile)).toString();
ObjectMapper mapper = new ObjectMapper();
className classForMapping = mapper.readValue(jsonData, className.class));
Im trying to understand the procedure to do what the title says.
Im doing this in java with Gson dependency.
I am getting information from another service I use, in JSON format. So I want to get that info, put some additional info in there (like date/time) and use it afterwards for searching purposes.
The procedure is :
Get the JSON info (lets say "id") and add it to the JSON file you have
Add more info to that JSON file (lets say "Date and time of upload")
Finally, save that updated JSON file
So I read the file:
JsonReader reader = new JsonReader(new FileReader(filename));
Do I have now to convert it to string, and then update the string, so I can finally write it back to json?
If it doesn't exist, I create an empty file and then, can I update it with Json/Gson data? or do I have to create a Json File?
try {
File jsonFile = new File("C:\\uploads\\datasets");
if (jsonFile.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
Excuse any newbie/stupid mistakes I've probably made, I'm trying to understand JSON. Actually, the philosophy behind it.
JSON stands for JavaScript Object Notation and it's nothing more than a way to format data.
Taken from here:
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is
realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array.
An ordered list of values. In most
languages, this is realized as an array, vector, list, or sequence.
To address your questions:
Get the JSON info (lets say "id") and add it to the JSON file you have
JsonReader reader = new JsonReader(new FileReader(inputFilename));
reader.beginArray();
reader.beginObject();
long id = -1;
while (reader.hasNext()) {
String value = reader.nextName();
if (value.equals("id")) {
id = reader.nextLong();
} else {
reader.skipValue();
}
reader.endObject();
reader.endArray();
Add more info to that JSON file (lets say "Date and time of upload")
This will get the format in YYYY.MM.DD-HH.MM.SS
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
Finally, save that updated JSON file
Create a JsonWriter.
JsonWriter writer = new JsonWriter(new FileWriter(outputFilename));
writer.beginArray();
writer.beginObject();
writer.name("id").value(id);
writer.name("timestamp").value(timestamp);
writer.endObject();
writer.endArray();
You can read more about JsonReader and JsonWriter here and here.
I am using Gson to write to and read from a file.
each time a user is created, I append a line which shows Username and ID.
I do this with the following (UserAccount being my pojo class, br is my buffered reader)::
UserAccount accountObj = new UserAccount();
FileWriter writer = new FileWriter("store.json", true);
accountObj.setUser(NewUserMethod());
accountObj.setID(NewIDMethod());
String json = gson.toJson(accountObj);
writer.write(json);
writer.close();
This makes the json file, which works - like this ::
{"USER":"noob123","ID":"99"}
When a new user does it, It will append the next line like so ::
{"USER":"noob123","ID":99}{"USER":"pro321","ID":100}
When I attempt to read this json file, I got the following error ::
"use JsonReader.setLenient(true) to accept malformed JSON at line 1, column 36"
So I understand it may be incorrect format and may need something like ::
{"ACCOUNT":{"USER":"noob123","ID":99},"ACCOUNT":{"USER":"pro321","ID":100}}
I have spent some time trying to create JSON in this format, or at least reference to the correct USER key when the matching USER name is used. Any help is greatly appreciated.
Thanks
You should use JSONArray to save this information into file. Then you can read content of file into list of UserAccount, add new object into this list.
Then you should override content of file.
E.g. :
UserAccount accountObj = new UserAccount();
accountObj.setUser(NewUserMethod());
accountObj.setID(NewIDMethod());
List<UserAccount> userAccounts = gson.fromJson(<jsonContent>, new TypeToken<List<UserAccount>>() {
}.getType());
userAccounts.add(accountObj);
String json = gson.toJson(userAccounts);
writer.write(json);
writer.close();
String json="{"FROM_JID":"6bc24cac4eaf304ce1731bd5aebe9b0419052701","TO_JID":"dfc8d53f402373a1d3622dde50e180b388b36bc1","TYPE_ID":"1","PLATFORM":"IOS","CONTENT":"{\"FROM_JID\":\"6bc24cac4eaf304ce1731bd5aebe9b0419052701\",\"FROM_HOST\":\"ssdevim.mtouche-mobile.com\",\"FROM_JNAME\":\"test1\",\"TO_JID\":\"dfc8d53f402373a1d3622dde50e180b388b36bc1\",\"TO_HOST\":\"ssdevim.mtouche-mobile.com\",\"MESSAGE_ID\":\"LiYaU-39\",\"MESSAGE_TYPE\":\"enc\",\"MESSAGE\":\"test1 has sent you an encrypted message.\",\"STAMP\":\"2015-11-12 12:04:54.252241\",\"BADGE\":3,\"CONTENT-AVAILABLE\":1,\"SOUND\":\"dafault\"}","DEVICE_ID":"AC53D4F0-DAAA-475E-9668-5E9E7485797C","PUSH_ID":"c9544c8db2117f02f3edc8af9058b3d54c15500302bf6f47c487193876f6dc23","CREATE_DATE":"2015-11-12","CREATE_TIME":"04:04:54"}";
JSONParser parser = new JSONParser();
Object obj = parser.parse(json);
but it showing error
First, this won't compile:
String json="{"FROM_JID":"6bc24cac4eaf304ce1731bd5aebe9b0419052701","TO_JID":"dfc8d53f402373a1d3622dde50e180b388b36bc1","TYPE_ID":"1","PLATFORM":"IOS","CONTENT":"{\"FROM_JID\":\"6bc24cac4eaf304ce1731bd5aebe9b0419052701\",\"FROM_HOST\":\"ssdevim.mtouche-mobile.com\",\"FROM_JNAME\":\"test1\",\"TO_JID\":\"dfc8d53f402373a1d3622dde50e180b388b36bc1\",\"TO_HOST\":\"ssdevim.mtouche-mobile.com\",\"MESSAGE_ID\":\"LiYaU-39\",\"MESSAGE_TYPE\":\"enc\",\"MESSAGE\":\"test1 has sent you an encrypted message.\",\"STAMP\":\"2015-11-12 12:04:54.252241\",\"BADGE\":3,\"CONTENT-AVAILABLE\":1,\"SOUND\":\"dafault\"}","DEVICE_ID":"AC53D4F0-DAAA-475E-9668-5E9E7485797C","PUSH_ID":"c9544c8db2117f02f3edc8af9058b3d54c15500302bf6f47c487193876f6dc23","CREATE_DATE":"2015-11-12","CREATE_TIME":"04:04:54"}";
You even can notice that its syntax is not highlighted properly.
You need to escape your quotes in order to make Java recognize it as a part of a string, but not your code:
String json="{\"FROM_JID\":\"6bc24cac4eaf304ce1731bd5aebe9b0419052701\",\"TO_JID\":\"dfc8d53f402373a1d3622dde50e180b388b36bc1\",\"TYPE_ID\":\"1\",\"PLATFORM\":\"IOS\",\"CONTENT\":\"{\\\"FROM_JID\\\":\\\"6bc24cac4eaf304ce1731bd5aebe9b0419052701\\\",\\\"FROM_HOST\\\":\\\"ssdevim.mtouche-mobile.com\\\",\\\"FROM_JNAME\\\":\\\"test1\\\",\\\"TO_JID\\\":\\\"dfc8d53f402373a1d3622dde50e180b388b36bc1\\\",\\\"TO_HOST\\\":\\\"ssdevim.mtouche-mobile.com\\\",\\\"MESSAGE_ID\\\":\\\"LiYaU-39\\\",\\\"MESSAGE_TYPE\\\":\\\"enc\\\",\\\"MESSAGE\\\":\\\"test1 has sent you an encrypted message.\\\",\\\"STAMP\\\":\\\"2015-11-12 12:04:54.252241\\\",\\\"BADGE\\\":3,\\\"CONTENT-AVAILABLE\\\":1,\\\"SOUND\\\":\\\"dafault\\\"}\",\"DEVICE_ID\":\"AC53D4F0-DAAA-475E-9668-5E9E7485797C\",\"PUSH_ID\":\"c9544c8db2117f02f3edc8af9058b3d54c15500302bf6f47c487193876f6dc23\",\"CREATE_DATE\":\"2015-11-12\",\"CREATE_TIME\":\"04:04:54\"}";
Second, if you already have a String and you want to convert it to byte[], why do you deserialize it? Just convert it to byte array:
byte[] bytes = json.getBytes();
The JSON example file consists of:
{
"1st_key": "value1",
"2nd_key": "value2",
"object_keys": {
"obj_1st": "value1",
"obj_2nd": "value2",
"obj_3rd": "value3",
}
}
I read the JSON file into a String with this StringBuilder method, in order to add the newlines into the string itself. So the String looks exactly like the JSON file above.
public String getJsonContent(String fileName) {
StringBuilder result = new StringBuilder("");
File file = new File(fileName);
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.append(line).append("\n");
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
Then I translate the JSON file into an Object using MongoDB API (with DBObject, BasicDBObject and util.JSON) and I call out the Object section I need to change, which is 'object_keys':
File jsonFile = new File(C:\\example.json);
String jsonString = getJsonContent(jsonFile.getAbsolutePath());
DBObject jsonObject = (DBObject)JSON.parse(jsonString);
BasicDBObject objectKeys = (BasicDBObject) jsonObject.get("object_keys");
Now I can write new values into the Object using the PUT method like this:
objectKeys.put("obj_1st","NEW_VALUE1");
objectKeys.put("obj_2nd","NEW_VALUE2");
objectKeys.put("obj_3rd","NEW_VALUE3");
! This following part not needed, check out my answer below.
After I have changed the object, I need to write it back into the json file, so I need to translate the Object into a String. There are two methods to do this, either one works.
String newJSON = jsonObject.toString();
or
String newJSON = JSON.serialize(jsonObject);
Then I write the content back into the file using PrintWriter
PrintWriter writer = new PrintWriter(C:\\example.json)
writer.print(newJSON);
writer.close();
The problem I am facing now is that the String that is written is in a single line with no formatting whatosever. Somewhere along the way it lost all the newlines. So it basically looks like this:
{"1st_key": "value1","2nd_key": "value2","object_keys": { "obj_1st": "NEW_VALUE1","obj_2nd": "NEW_VALUE2","obj_3rd": "NEW_VALUE3", }}
I need to write the JSON file back in the same format as shown in the beginning, keeping all the tabulation, spaces etc.
Is this possible somehow ?
When you want something formatted the way you said it is addressed as writing to a file in a pretty/beautiful way. For example: Output beautiful json. A quick search on google found what i believe to solve your problem.
Solution
You're going to have to use a json parser of some sort. I personally prefer org.json and would recommend it if you are manipulating the json data, but you may also like json-io which is really good for json serialization with no external dependencies.
With json-io, it's as simple as
String formattedJson = JsonWriter.formatJson(jsonObject.toString())
With org.json, you simply pass an int to the toString method.
Thanks Saraiva, I found a surprisingly simple solution by Googling around with the words 'pretty printing JSON' and used the Google GSON library. I downloaded the .jar and added it to my project in Eclipse.
These are the new imports I needed:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Since I already had the JSON Object (jsonObject) readily available from my previous code, I only needed to add two new lines:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String newJSON = gson.toJson(jsonObject);
Now when I use writer.print(newJSON); it will write the JSON in the right format, beautifully formatted and indented.