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

  • Home
  • SEARCH
  • 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 8876943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:20:31+00:00 2026-06-14T19:20:31+00:00

I have two json files that I’m trying to merge. The JSONs have different

  • 0

I have two json files that I’m trying to merge. The JSONs have different formatting (see below). I’d like to merge records, so [0] from file one and [0] from file two would become one record [0] in the new merged file.

The first JSON (file_a.json), appears like so:

{

    "query": {
        "count": 4,
        "created": "2012-11-21T23:07:00Z",
        "lang": "en-US",
        "results": {
            "quote": [
                {
                    "Name": "Bill",
                    "Age": "46",
                    "Number": "3.55"
                },
                {
                    "Name": "Jane",
                    "Age": "33",
                    "Number": nil
                },
                {
                    "Name": "Jack",
                    "Age": "55",
                    "Number": nil
                },
                {
                    "Name": "Xavier",
                    "Age": nil,
                    "Number": "153353535"
                }
            ]
        }
    }

}

The second JSON (file_b.json) appears like so:

[

    {
        "Number2": 25253,
        "Number3": 435574,
        "NAME": "Bill"
    },
    {
        "Number2": 345353,
        "Number3": 5566,
        "NAME": "Jane"
    },
    {
        "Number2": 56756,
        "Number3": 232435,
        "NAME": "Jack"
    },
    {
        "Number2": 7457,
        "Number3": 45425,
        "NAME": "Xavier"
    }
]

None of the keys are the same in both JSONs (well, actually “Name” is a key in both, but in the first the key is “Name” and in the second its “NAME” – just so I can check that the merge works correctly – so I want “Name” and “NAME” in the final JSON), the first record in the first file matches with the first record in the second file, and so on.

So far, I tried merging like this:

merged = %w[a b].inject([]) { |m,f| m << JSON.parse(File.read("file_#{f}.json")) }.flatten

But this of course merged them, but not how I wanted them merged (they are merged sucessively, and because of the different formatting, it gets quite ugly).

I also tried merging like this:

a = JSON.parse(File.read("file_a.json"))
b = JSON.parse(File.read("file_b.json"))

merged = a.zip(b)

Came closer but still not correct and the formatting was still horrendous.

In the end, what I want is this (formatting of second JSON – headers from first JSON can be junked):

[

    {
        "Name": "Bill",
        "Age": 46,
        "Number": 3.55,
        "Number2": 25253,
        "Number3": 435574,
        "NAME": "Bill"
    },
    {
        "Name": "Jane",
        "Age": 33,
        "Number": nil,
        "Number2": 345353,
        "Number3": 5566,
        "NAME": "Jane"
    },
    {
        "Name": "Jack",
        "Age": 55,
        "Number": nil,
        "Number2": 56756,
        "Number3": 232435,
        "NAME": "Jack"
    },
    {
        "Name": "Xavier",
        "Age": nil,
        "Number": 153353535,
        "Number2": 7457,
        "Number3": 45425,
        "NAME": "Xavier"
    }
]

Any help is appreciated. Thanks a lot.

  • 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-06-14T19:20:32+00:00Added an answer on June 14, 2026 at 7:20 pm

    Hеllo, seems format changed from last time 🙂

    UPDATE: more readable version that also convert corresponding values to integers/floats:

    require 'json'
    require 'ap'
    
    a = JSON.parse(File.read('./a.json'))['query']['results']['quote'] rescue []
    b = JSON.parse(File.read('./b.json'))
    
    final = []
    a.each_with_index do |ah,i|
      unless bh = b[i]
        bh = {}
        puts "seems b has no #{i} key, merging skipped"
      end
    
      final << ah.merge(bh).inject({}) do |f, (k,v)|
        if v.is_a?(String)
          if v =~ /\A\d+\.\d+\Z/
            v = v.to_f
          elsif v =~ /\A\d+\Z/
            v = v.to_i
          end
        end
        f.update k => v
      end
    end
    ap final
    

    will display:

    [
      [0] {
           "Name" => "Bill",
          "Age" => 46,
         "Number" => 3.55,
        "Number2" => 25253,
        "Number3" => 435574,
           "NAME" => "Bill"
      },
      [1] {
           "Name" => "Jane",
          "Age" => 33,
         "Number" => nil,
        "Number2" => 345353,
        "Number3" => 5566,
           "NAME" => "Jane"
      },
      [2] {
           "Name" => "Jack",
          "Age" => 55,
         "Number" => nil,
        "Number2" => 56756,
        "Number3" => 232435,
           "NAME" => "Jack"
      },
      [3] {
           "Name" => "Xavier",
          "Age" => nil,
         "Number" => 153353535,
        "Number2" => 7457,
        "Number3" => 45425,
           "NAME" => "Xavier"
      }
    ]
    

    Here is a working demo

    Btw, your json is a bit wrong in both files.

    See the fixed versions here and here

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

Sidebar

Related Questions

I have multiple JSON files that I would like to parse, edit, and merge
Suppose I have two json files. I would like to be able to load
I have two arrays in JSON something like: proj1 proj1 taska taska charge1 charge1
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
I am trying to build JSON from two fields. Say, I have a list
I just ran into something weird. I have two JSON arrays which holds different
I have two .json files data_prod_ind.json {articles: [ { title:bla bla } , {
I have a json file that's like for e.g. [{fu: thejimjams, su: 232104580}, {fu:
I have two files. location.php, that outputs this: [[javascript],[PHP]] and in another file: <script
I have two files, one containing an array in PHP that is echo ed

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.