1

I am building JSON Web Token (JWT) using JSONObject and JSONArray. When creating a payload I need to match the following part (array that contains an array)

"Taxes":
[{
   "VAT": [{ "TaxRate": "A", "Amount": 100 }, { "TaxRate": "B", "Amount": 300 }]
]}

I've tried to implement it with the following code

JSONArray taxes= new JSONArray();
JSONArray vat = new JSONArray();
vat.add(new JSONObject()
        .put("TaxRate", "A")
        .put("Amount", 100).toString());
vat.add(new JSONObject()
        .put("TaxRate", "B")
        .put("Amount", 300).toString());
taxes.add(new JSONObject()
         .put("VAT", vat).toString());

Problems

If toString() methods are not called at all the result is [{}]. If they are not called when adding to vat array the result is ["{\"VAT\":\"[{},{}]\"}"].

The final result of the taxes array string when printed to the console is ["{\"VAT\":\"[\\\"{\\\\\\\"Amount\\\\\\\":100,\\\\\\\"TaxRate\\\\\\\":\\\\\\\"A\\\\\\\"}\\\",\\\"{\\\\\\\"Amount\\\\\\\":300,\\\\\\\"TaxRate\\\\\\\":\\\\\\\"B\\\\\\\"}\\\"]\"}"].

However, the vat array contains elements without backslashes, eg. {"Amount":100,"TaxRate":"A"}. The taxes array has one entry and it looks like {"VAT":"[\"{\\\"Amount\\\":100,\\\"TaxRate\\\":\\\"A\\\"}\",\"{\\\"Amount\\\":300,\\\"TaxRate\\\":\\\"B\\\"}\"]"}

Question

What is the correct way to build the structure I am trying to create?

It looks like the toString() method is escaping the quotes and adding the slashes. That kind of payload cannot be used in a request as the server side appliaction cannot parse it.

3
  • What kind of framework do you use for serializing objects to json? Commented Sep 17, 2015 at 8:59
  • 1
    Maybe try to create each JSONobject separately (instead of adding it directly) and log the object to the console. Using .toString() with adding the objects is not good and basically destroys the inner objects. Commented Sep 17, 2015 at 9:01
  • @ErnestSadykov: JSONObject is json-jena 1.0 and the JSONArray is nimbus-jose-jwt 3.4. Commented Sep 17, 2015 at 9:02

1 Answer 1

1

Nimbus Jose uses json-smart internally. Therefore, import statements should look like this:

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

The code for creating json structure:

JSONArray taxes= new JSONArray();
JSONArray vat = new JSONArray();
JSONObject a = new JSONObject();
a.put("TaxRate", "A");
a.put("Amount", 100);
vat.add(a);

JSONObject b = new JSONObject();
b.put("TaxRate", "B");
b.put("Amount", 300);
vat.add(b);

JSONObject vatObject = new JSONObject();
vatObject.put("VAT", vat);
taxes.add(vatObject);

JSONObject taxesObject = new JSONObject();
taxesObject.put("Taxes", taxes);

// generate string:
System.out.println(taxesObject.toJSONString());

// or create JWT:
new JWSObject(new JWSHeader(...), new Payload(taxesObject))

Output:

{"Taxes":[{"VAT":[{"Amount":100,"TaxRate":"A"},{"Amount":300,"TaxRate":"B"}]}]}
Sign up to request clarification or add additional context in comments.

1 Comment

Bummer, I was using mixed imports. However, I decided to use everything from org.json package as I like the chaining option.

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.