0

I have an array of hash as shown here. I want to merge the values of some fields with custom seprators. Here, i show only two hashes in the array, it is possible to have more. But, they are always in same sequence as shown here.

{
"details": [
{
  "place": "abc",
  "group": 3,
  "year": 2006,
  "id": 1304,
  "street": "xyz 14",
  "lf_number": "0118",
  "code": 4433,
  "name": "abc coorperation",
  "group2": 3817,
  "group1": 32,
  "postal_code": "22926",
  "status": 2
},
{
  "place": "cbc",
  "group": 2,
  "year": 2007,
  "id": 4983,
  "street": "mnc 14",
  "lf_number": "0145",
  "code": 4433,
  "name": "abc coorperation",
  "group2": 3817,
  "group1": 32,
  "postalcode": "22926",
  "status": 2
}
],
"@timestamp": "2017-09-04",
"parent": {
  "child": [
  {
    "w_2": 0.5,
    "w_1": 0.1,
    "id": 14226,
    "name": "air"
  },
  {
    "w_2": null,
    "w_1": 91,
    "id": 25002,
    "name": "Water"
  }]
},
"p_name": "anacin",
"@version": "1",
 "id": 28841
}

I want to edit the details. I want to construct new fields.

Field 1)  coorperations: (details.name | details.postal_code details.street ; details.name | details.postal_code details.street)

Output:
Coorperations: (abc coorperation |22926 xyz 14; abc coorperation | 22926 mnc 14)

Field 2) access_code: (details.status-details.id-details.group1-details.group2-details.group(always two digit)/details.year(only last two digits); details.status-details.id-details.group1-details.group2-details.group(always two digit)/details.year(only last two digits))

Output: access_code (2-32-3817-03-06; 2-32-3817-02-07)

How can I achieve this for all the values in details. Here is how final results should look like.

{
"@timestamp": "2017-09-04",
"parent": {
"child": [
 {
   "w_2": 0.5,
   "w_1": 0.1,
   "id": 14226,
   "name": "air"
  },
  {
   "w_2": null,
   "w_1": 91,
   "id": 25002,
   "name": "Water"
  }]
},
"p_name": "anacin",
"@version": "1",
"id": 28841,
"Coorperations" : "abc coorperation |22926 xyz 14; abc coorperation | 22926 mnc 14",
"access_code" : "2-32-3817-03-06; 2-32-3817-02-07"
}

1 Answer 1

0

You can try to run this code in rails console with hash is your json:

new_hash = hash.except(:details)
coorperations = ""
access_code = ""
elements = hash[:details]
elements.each do |element|
    coorperations = "#{coorperations}#{if coorperations.present? then '; ' else '' end}#{element[:name]} | #{element[:postal_code]} #{element[:street]}"
    access_code = "#{access_code}#{if access_code.present? then '; ' else '' end}#{element[:status]}-#{element[:id]}-#{element[:group1]}-#{element[:group2]}-#{element[:group1]}-#{element[:group]}"
end
new_hash.merge!(Coorperations: coorperations)
new_hash.merge!(access_code: access_code)
new_hash
Sign up to request clarification or add additional context in comments.

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.