Format response in one level of depth


#1

Hi to all, I need advice…
Is there a way to format the following response into one-level of depth (i mean for pivot data or how to remove pivot key and put its properties one level up):

{
    "data": [
        {
            "id": 1,
            "company_id": 1,
            "permalink": "tesar-m-z",
            "workplace_name": "Tesar (m/ž)",
            "description": "",
            "job_requirements": "",
            "other_languages": "",
            "required_work_experience": 5,
            "special_benefits": "",
            "salary_min": 4000,
            "salary_max": 10000,
            "job_contest_deadline": "2018-04-10 00:00:00",
            "activate_at": "2018-03-15 00:00:00",
            "workplace_location": "Osijek",
            "state": "Hrvatska",
            "keywords": "tesar,stolar",
            "job_designation": null,
            "qualification_id": 3,
            "job_url": "www.stolar.hr",
            "company": {
                "id": 1,
                "company_name": "Company1",
                "address": "Osječka 25",
                "postal_code": "10000",
                "city": "Osijek",
                "country": "Hrvatska",
                "phone": "08333883",
                "fax_number": "08333883",
                "email": "employer1@gmail.com",
                "contact": "Josip",
                "website_url": "www.company1.hr"
            },
            "job_categories": [
                {
                    "id": 6,
                    "name": "Graditeljstvo i geodezija",
                    "pivot": {
                        "category_id": 6,
                        "job_id": 1
                    }
                },
                {
                    "id": 15,
                    "name": "Zanatska zanimanja i proizvodnja",
                    "pivot": {
                        "category_id": 15,
                        "job_id": 1
                    }
                }
            ],
            "job_counties": [
                {
                    "id": 10,
                    "name": "034 - Požeško-slavonska županija",
                    "pivot": {
                        "county_id": 10,
                        "job_id": 1
                    }
                },
                {
                    "id": 7,
                    "name": "031 - Osječko-baranjska županija",
                    "pivot": {
                        "county_id": 7,
                        "job_id": 1
                    }
                }
            ],
            "job_qualification": {
                "id": 3,
                "name": "Srednja škola"
            },
            "job_driving_licenses": [
                {
                    "id": 4,
                    "name": "B",
                    "pivot": {
                        "driving_license_id": 4,
                        "job_id": 1
                    }
                }
            ],
            "job_employment_types": [
                {
                    "id": 3,
                    "name": "Rad na određeno vrijeme",
                    "pivot": {
                        "employment_type_id": 3,
                        "job_id": 1
                    }
                },
                {
                    "id": 4,
                    "name": "Stalni radni odnos",
                    "pivot": {
                        "employment_type_id": 4,
                        "job_id": 1
                    }
                }
            ],
            "job_comp_programs": [
                {
                    "id": 2,
                    "name": "Internet",
                    "pivot": {
                        "computer_program_id": 2,
                        "job_id": 1,
                        "level": "osnovno",
                        "id": 1
                    }
                },
                {
                    "id": 1,
                    "name": "MS Office",
                    "pivot": {
                        "computer_program_id": 1,
                        "job_id": 1,
                        "level": "osnovno",
                        "id": 2
                    }
                }
            ],
            "job_languages": [
                {
                    "lang_option_id": 2,
                    "name": "Engleski",
                    "pivot": {
                        "lang_option_id": 2,
                        "job_id": 1,
                        "level": "osnovno"
                    }
                }
            ]
        }
    ],
    "message": "",
    "debug": {
        "untranslatedMsg": ""
    }
}

The code that generates previous response:

async getJobPublic({ request, response, params }) {

    const job = await Job
      .query()
      .with('company', (q) => {
        q.select(['id', 'company_name', 'address', 'postal_code',
                  'city', 'country', 'phone', 'fax_number', 'email',
                  'contact', 'website_url'])
      })
      .with('job_categories', (q) => {
        q.select('id', 'name')
      })
      .with('job_counties', (q) => {
        q.select('id', 'name')
      })
      .with('job_qualification', (q) => {
        q.select('id', 'name')
      })
      .with('job_driving_licenses', (q) => {
        q.select('id', 'name')
      })
      .with('job_employment_types', (q) => {
        q.select('id', 'name')
      })
      .with('job_comp_programs', (q) => {
        q.select('id', 'name')
      })
      .with('job_languages', (q) => {
        q.select('lang_option_id', 'name')
      })
      .where('permalink', params.permalink)
      .where('id', params.id)
      .select(['id', 'company_id', 'permalink',
               'workplace_name', 'description',
               'job_requirements', 'other_languages', 'required_work_experience', 'special_benefits',
               'salary_min', 'salary_max', 'job_contest_deadline', 'activate_at', 'workplace_location',
               'state', 'keywords', 'is_HZZ_job', 'job_designation', 'qualification_id', 'job_url'])
      .fetch()


    response.ok(job)
  }

#2

Hi,

I’ve answered something similar here

Let me know if it helps :smiley:


#3

I see… You just mapped the resulting json into desired object shape. I’ll look into it. Thanks @antwaanh


#4

No problems,

You might also consider creating a custom serializer


#5

Sure, I’ll try that. Thanks again