4

I have JSON with one object for each site's traffic for 3 dates.

I want to merge these three objects together on "Date". Example below.

I'm using Ruby. What is the easiest way to do this?

Start JSON:

 [  
   {  
      "Google":[  
         {  
            "Date":"2015-01-01",
            "Value":100
         },
         {  
            "Date":"2015-02-01",
            "Value":200
         },
         {  
            "Date":"2015-03-01",
            "Value":300
         }
      ]
   },
   {  
      "Yahoo":[  
         {  
            "Date":"2015-01-01",
            "Value":1200
         },
         {  
            "Date":"2015-02-01",
            "Value":1300
         },
         {  
            "Date":"2015-03-01",
            "Value":1400
         }
      ]
   },
   {  
      "Bing":[  
         {  
            "Date":"2015-01-01",
            "Value":500
         },
         {  
            "Date":"2015-02-01",
            "Value":600
         },
         {  
            "Date":"2015-03-01",
            "Value":700
         }
      ]
   }
]

End JSON:

[
  {  
    "Date":"2015-01-01",
    "Google":100,
    "Yahoo":1200,
    "Bing":500
  },
  {  
    "Date":"2015-01-02",
    "Google":200,
    "Yahoo":1200,
    "Bing":600
  },
  {  
    "Date":"2015-01-03",
    "Google":300,
    "Yahoo":1400,
    "Bing":700
  }
]
6
  • i would use map reduce in mongo for that, but if you want in Ruby, think about ruby-doc.org/core-2.2.2/Enumerable.html#method-i-group_by and JSON parse/dump Commented May 12, 2015 at 20:09
  • I don't see any JSON things in the question. Commented May 12, 2015 at 20:28
  • @undur_gongor Author mentioned that he has a json data. Commented May 12, 2015 at 20:29
  • @CodeGroover: But all the presented data are just plain Ruby data (arrays, hashes, strings), no JSON. Commented May 12, 2015 at 20:34
  • 1
    You're both right :) - The data started as JSON, then I just did JSON.parse, so yes the question is really just concerned with arrays of hashes. The JSON tag just helps people that are looking to merge similar JSON structures. Commented May 12, 2015 at 20:46

2 Answers 2

6
result = array.inject({}) do | a, e | 
  site, data = e.first
  data.each do | x | 
    a[x[:Date]] ||= {}
    a[x[:Date]][site] = x[:Value]
  end
  a
end

gives you a hash with the dates as keys. This can be transformed to the array by:

result.map { | k, v | v.update(:Date => k) }
Sign up to request clarification or add additional context in comments.

4 Comments

And accumulation part a[x[:Date]][site] ? a[x[:Date]][site] += x[:Value] : x[:Value] and also the target result is array
also after JSON parsing it has keys as Strings not a Symbols.
@CodeGroover: You are right with the array-vs-hash remark. Thank you.
sorry, no accumulation of course, it is just a transformation.
0

Assuming exactly this structure, what might work is

results = [] 

your_array[0]['Google'].each_with_index do |item, index|
  date = item['Date']

  provider_values = your_array.inject({}) do |memo, current|
    provider = current.keys[0]
    value = current[provider][index]['Value']

    memo[provider] = value
    memo
  end 
  results.push({'Date' => date}.merge(provider_values))
end

I'm currently on Windows so I can't be 100% sure of correctness, however fixing any syntax errors should be easy.

Comments

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.