0

I have two json string coresponding to "sig" and "enc".

String a={ "kty": "someText", "e": "someText", "use": "sig", "kid": "someText", "alg": "someText", "n": "someText"}

String b= { "kty": "someText", "e": "someText", "use": "enc", "kid": "someText", "alg": "someText", "n": "someText" }

I need to combine them both like this:

{ "keys": [ { "kty": "someText", "e": "someText", "use": "sig", "kid": "someText", "alg": "someText", "n": "someText" }, { "kty": "someText", "e": "someText", "use": "enc", "kid": "someText", "alg": "someText", "n": "someText" } ] }

The resulting JSON is nothing but JWKS which includes both sig and enc public keys. I have replaced the actual values for these parameters.

Q2Dev
  • 85
  • 1
  • 9

2 Answers2

2

Try the below code :

public void test(){
        String a="{ \"kty\": \"someText\", \"e\": \"someText\", \"use\": \"sig\", \"kid\": \"someText\", \"alg\": \"someText\", \"n\": \"someText\"}";
        String b= "{ \"kty\": \"someText\", \"e\": \"someText\", \"use\": \"enc\", \"kid\": \"someText\", \"alg\": \"someText\", \"n\": \"someText\" }";
        JSONObject jsonObject = new JSONObject(a);
        JSONObject jsonObject1 = new JSONObject(b);
        JSONArray objects1 = new JSONArray();
        objects1.put(jsonObject);
        objects1.put(jsonObject1);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("keys",objects1);
        System.out.println(jsonObject2);
    }

Output:

{"keys":[{"kty":"someText","e":"someText","use":"sig","kid":"someText","alg":"someText","n":"someText"},{"kty":"someText","e":"someText","use":"enc","kid":"someText","alg":"someText","n":"someText"}]}

Library used : org.json

Prog_G
  • 1,539
  • 1
  • 8
  • 22
  • I have tried the same, but my jsonObject will have escape characters in the output. Any ide on how to resolve it? – Q2Dev Dec 21 '21 at 07:29
  • @yuhubug Are you sure those escape characters ware not present before *merging* those two JSON objects? If they ware not could you provide example of *correct* JSONs, what you expected and what you got instead? – Pshemo Dec 21 '21 at 08:16
  • @yuhubug you will be having an escape characters in the input JSON (a & b)? – Prog_G Dec 21 '21 at 09:41
1

Please refer to the following logic implemented with org.json library.

public void generateJSONObject() {
    String a = "{ \"kty\": \"someText\", \"e\": \"someText\", \"use\": \"sig\", \"kid\": \"someText\", \"alg\": \"someText\", \"n\": \"someText\"}";
    String b = "{ \"kty\": \"someText\", \"e\": \"someText\", \"use\": \"enc\", \"kid\": \"someText\", \"alg\": \"someText\", \"n\": \"someText\" }";
    JSONObject jsonObject1 = new JSONObject(a);
    JSONObject jsonObject2 = new JSONObject(b);
    JSONObject jsonObject = new JSONObject();
    jsonObject.append("keys", jsonObject1);
    jsonObject.append("keys", jsonObject2);
    System.out.println(jsonObject);
  }
  • why the `parser.parse(a).toString()`? – Maurice Perry Dec 21 '21 at 07:06
  • Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject constructor as defined in org.json.JSONObject, public JSONObject(String source) throws JSONException { this(new JSONTokener(source)); } – Bipin Kumar Chaurasia Dec 21 '21 at 07:17
  • 1
    @BipinKumarChaurasia yes: just pass `a` or `b` to the constructor. – Maurice Perry Dec 21 '21 at 07:23
  • @BipinKumarChaurasia I have tried the same, but my jsonObject will have escape characters in the output. Any ide on how to resolve it? – Q2Dev Dec 21 '21 at 07:29
  • No, you won't be having the escape quotes as the following output had been printed after executing above function, `{"keys":[{"kty":"someText","e":"someText","use":"sig","kid":"someText","alg":"someText","n":"someText"},{"kty":"someText","e":"someText","use":"enc","kid":"someText","alg":"someText","n":"someText"}]} ` – Bipin Kumar Chaurasia Dec 21 '21 at 07:30