Testing Excellence

  • QA Basics
  • Technical QA
  • Agile Testing
  • Questions
  • Quizzes
Home » Java » How to Encode and Decode JSON Byte Array

How to Encode and Decode JSON Byte Array

Updated: December 2, 2018 - Amir Ghahrai

The typical way to send binary in JSON is to base64 encode it. Java provides different ways to Base64 encode and decode a byte[]. One of these is DatatypeConverter.

Suppose we have a JSON Array as listed below:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Encode JSON as Base64

To encode the above JSON, we would use

String base64Encoded = DatatypeConverter.printBase64Binary(jsonBytes);

Decode Base64 JSON

To decode a base64 encoded JSON, we would use

byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);

Example code:

import javax.xml.bind.DatatypeConverter;

public class JsonEncodeDecode {

    public static void main(String[] args) {
        String json = "{\"menu\": {\n" +
                "  \"id\": \"file\",\n" +
                "  \"value\": \"File\",\n" +
                "  \"popup\": {\n" +
                "    \"menuitem\": [\n" +
                "      {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n" +
                "      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" +
                "      {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" +
                "    ]\n" +
                "  }\n" +
                "}}";

        byte[] bytes = json.getBytes();

        String base64Encoded = DatatypeConverter.printBase64Binary(bytes);
        System.out.println("Encoded Json:\n");
        System.out.println(base64Encoded + "\n");

        byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);
        System.out.println("Decoded Json:\n");
        System.out.println(new String(base64Decoded));
    }
}

Output:
Encoded JSON

eyJtZW51IjogewogICJpZCI6ICJmaWxlIiwKICAidmFsdW

Decoded JSON

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Note: the encoded JSON is truncated for sake of neatness, otherwise it’s a very long string.

How to Parse JSON Response with REST-assured

Tags: Java, JSON, Technical Skills





Selected Articles

  • Test Automation Problems
  • Test Automation Strategy
  • Agile Test Strategy example
  • How QAs add value in agile
  • Agile without automation
  • How agile killed managers
  • Agile testing challenges
  • Testing e-commerce websites
  • Role of QA manager in agile
  • Are you a good agile tester?
  • BDD tips and best practices
  • Myths of test automation
  • Test automation tips
  • Test automation pros & cons






Favorites Links

Java for Testers

Selenium Tutorial

Testing in Agile and DevOps

 

Copyright © 2019 All rights reserved · www.testingexcellence.com

  • About
  • Contact
  • Subscribe
  • Privacy Policy
  • Terms of Use