Alex Thai
Alex Thai

Reputation: 176

Best Practice / Design Patterns for data reformat

My project is a micro-services that connect two major services, my project fetches data from one server, format the data, and then use the data to generate an XML file, and then upload the XML to another service. I'm just wondering if there is any design pattern for this kind of micro-services.

this is the JSON received from the backend server:

{
    "employee_id": 100464,
    "organization_id": 93,
    "start_date": "2018-09-05",
    "first_name": "Tom",
    "departments": [
        {
            "id": 2761,
            "name": "Sale",
            "organization_id": 93
        },
        {
            "id": 2762,
            "name": "Product",
            "organization_id": 93
        }
    ],
    "primary_department": {
        "id": 2761,
        "name": "Product",
        "organization_id": 93
    }
}

This is the data format I want for, so I need to do some data formatting:

{
    "employee_id": 100464,
    "organization_id": 93,
    "first_name": "Tom",
    "target_department": {
        "department_id": 2761,
        "name": "Product",
        "organization_id": 93,
        "is_primary_department": true
    }
}

the logic to determine the target_department is

departments = hsh.delete :departments
primary_department = hsh.delete :primary_department
hsh[:target_department] = departments.select do |department|
  department[:id] ==another_obj[:group_id]
end.first
hsh[:target_department][:is_home_department] = (hsh[:target_department][:id] == primary_department[:id])
hsh[:target_department][:department_id] = hsh[:target_department].delete :id

As you can see, I need to remove, rename, and reformat attributes and the structure of the data.

During the process, there are many potential issues: attributes not existed? My question is what's the best practice for dealing with this issue from programming designing perspective?

I am using Rails, so any good gem or project is dealing with a similar issue?

Upvotes: 1

Views: 106

Answers (2)

iam1me
iam1me

Reputation: 203

Working directly with json/xml type data programmatically is often tedious. I once worked at a company that did this EVERYWHERE and it was painful. I would suggest deserializing your data into an object graph representation. Also have a result class that can be constructed by querying/processing your input object graph. Then just serialize your result object back to json at the end.

You can likely find a built-in or open source solution for handling the serialization and deserialization for you.

Upvotes: 0

jvillian
jvillian

Reputation: 20263

I don't know about best practices, but this ought to do what you want.

{
  "employee_id": 100464,
  "organization_id": 93,
  "start_date": "2018-09-05",
  "first_name": "Tom",
  "departments": [
    {
      "id": 2761,
      "name": "Sale",
      "organization_id": 93
    },
    {
      "id": 2762,
      "name": "Product",
      "organization_id": 93
    }
  ],
  "primary_department": {
    "id": 2761,
    "name": "Product",
    "organization_id": 93
  }
}.with_indifferent_access.except(:start_date, :departments).transform_keys do |k|
  k == 'primary_department' ? 'target_department' : k
end.tap do |hsh|
  if hsh['target_department']
    hsh['target_department']['is_primary_department'] = true
    hsh['target_department']['department_id'] = hsh['target_department']['id']
    hsh['target_department'].delete('id')
  end
  puts hsh
end

In console, this will return:

{
  "employee_id"=>100464,
  "organization_id"=>93,
  "first_name"=>"Tom",
  "target_department"=>{
    "name"=>"Product", 
    "organization_id"=>93, 
    "is_primary_department"=>true, 
    "department_id"=>2761
  }
}

BTW, in your example output, you show the target_department name as "aProduct". Is that a typo?

Upvotes: 1

Related Questions