2

I have below situation. It needs to be implemented in Java.

  1. Take input from a text file, convert the content into a byte array.
  2. 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.

1
  • Common way to include binary data in JSON is to encode it as a Base 64 string. Did the person who assigned this task give any indication that's what they want to see? Commented Sep 19, 2018 at 21:52

2 Answers 2

1
  1. You can simply convert the byte array ip using ip.toString()
  2. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Regarding, ip.toString() -- the toString() method of an array just prints the element type and object ID, like: "[B@101df177". But I guess that's one way to "include the byte array in the JSON object" :-D
The json i am creating is used to send a request to a webservice. The webservice accepts input in form of byte array. So the conversion to String will not help i guess. Any other way to add the byte array as is in the JSON object?
0

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());
}

2 Comments

If you do this, it's good practice to specify the character encoding when creating the String. Also, steer clear of FileWriter since it always uses the platform default character encoding. Instead, wrap a FileOutputStream in an OutputStreamWriter (which takes the encoding as a constructor argument).
The json i am creating is used to send a request to a webservice. The webservice accepts input in form of byte array. So the conversion to String will not help i guess. Any other way to add the byte array as is in the JSON object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.