Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 664751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:38:02+00:00 2026-05-13T23:38:02+00:00

I am consuming some JSON from two different sources, I end up with two

  • 0

I am consuming some JSON from two different sources, I end up with two JSONObjects and I’d like to combine them into one.

Data:

"Object1": {
    "Stringkey":"StringVal",
    "ArrayKey": [Data0, Data1]
}

"Object2": {
    "Stringkey":"StringVal",
    "Stringkey":"StringVal",
    "Stringkey":"StringVal",
}

Code, using http://json.org/java/ library:

// jso1 and jso2 are some JSONObjects already instantiated
JSONObject Obj1 = (JSONObject) jso.get("Object1");
JSONObject Obj2 = (JSONObject) jso.get("Object2");

So in this situation I’d like to combine Obj1 and Obj2, either to make a totally new JSONObject or concat one to the other. Any ideas besides pulling them all apart and individually adding in by puts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:38:03+00:00Added an answer on May 13, 2026 at 11:38 pm

    If you want a new object with two keys, Object1 and Object2, you can do:

    JSONObject Obj1 = (JSONObject) jso1.get("Object1");
    JSONObject Obj2 = (JSONObject) jso2.get("Object2");
    JSONObject combined = new JSONObject();
    combined.put("Object1", Obj1);
    combined.put("Object2", Obj2);
    

    If you want to merge them, so e.g. a top level object has 5 keys (Stringkey1, ArrayKey, StringKey2, StringKey3, StringKey4), I think you have to do that manually:

    JSONObject merged = new JSONObject(Obj1, JSONObject.getNames(Obj1));
    for(String key : JSONObject.getNames(Obj2))
    {
      merged.put(key, Obj2.get(key));
    }
    

    This would be a lot easier if JSONObject implemented Map, and supported putAll.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some information - I'm creating an autocomplete which gets the data from a json
I have some code I've written in PHP for consuming our simple webservice, which
I'm trying to use JQuery's $.getJSON to access Model data from the Controller, and
I want to run a relatively time consuming script based on some form input,
Let's say you have some time-consuming work to do when a module/class is first
I have some data that's currently stored in an Excel workbook. It makes sense
I have some trivial code that looks like this: SortedDictionary<String, long> d = new
First, 2 common (basic) approaches: # returning from some FoosController method respond_to do |format|
I'm planning a project which will uses JSON objects to display data on andorid
I am looking for some alternatives of consuming a SOAP web service in java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.