Wednesday, April 12, 2017

Transform JSON data with index and send

I want to transform the the json data i'm getting and send it to requested app so that they can get the key values easily

//this is what i'm getting in postman

{
    "dropdown_data": {
        "driving_regions": [{
            "name": "East India"
        }, {
            "name": "West India"
        }],
        "employment_types": [{
            "type": "Owner"
        }, {
            "type": "Other"
        }],
        "event_names": [{
            "name": "Transport Mitra"
        }, {
            "name": "Other"
        }],
        "languages": [{
            "name": "English"
        }, {
            "name": "Spanish"
        }, {
            "name": "Hindi"
        }]
    },
    "user_details": {

     }
}

//i want to transform like the below

{
    "0": "dropdown_data",
    "dropdown_data": {
        "0": "driving_regions",
        "driving_regions": [{
            "name": "East India"
        }, {
            "name": "West India"
        }],
        "1": "employment_types",
        "employment_types": [{
            "type": "Owner"
        }, {
            "type": "Other"
        }],
        "2": "event_names",
        "event_names": [{
            "name": "Transport Mitra"
        }, {
            "name": "Other"
        }],
        "3": "languages",
        "languages": [{
            "name": "English"
        }, {
            "name": "Spanish"
        }, {
            "name": "Hindi"
        }]
    },
    "1": "user_details",
    "user_details": {

    }
}

in my controller i'm just getting the data and sotring in variables and then i'l just return as json as shown below

   return Response::json([
        'dropdown_data' => [
            'castes' => $castes->all(),
            'driving_regions' => $driving_regions->all(),
            'employment_types' => $employment_types->all(),
            'event_names' => $event_types->all(),
            'languages' => $languages->all()
        ],
            'user_details' => $user_details,
    ]);

Thank You



via Mr Robot

Advertisement