Naresh
Naresh

Reputation: 2781

How to merge JSON two indexes and then Parse information

I am trying to iterate both index of JSON(Players and Buildings), so that i can a get new result in jQuery

I have two index of JSON one having information of Players and second index having information of Building related Player.

I want to Parse it so that i can get Player and its building name.

My Actual JSON result

{
        "Players": [
            {
                "id": "35",
                "building_id": "8",
                "room_num": "101",
            },
            {
                "id": "36",
                "building_id": "9",
                "room_num": "102",
            },
            {
                "id": "37",
                "building_id": "10",
                "room_num": "103",
            },
            {
                "id": "38",
                "building_id": "11",
                "room_num": "104",
            }
        ],
        "Buildings": [
            {
                "id": "8",
                "name": "ABC"
            },
            {
                "id": "9",
                "name": "DEF"
            },
            {
                "id": "10",
                "name": "GHI"
            },
            {
                "id": "11",
                "name": "JKL"
            }
        ]
    }

Need this

 "information": [
                {
                    "player_id": "35",
                    "Buildings_name": "ABC"
                },
                {
                    "player_id": "36",
                    "Buildings_name": "DEF"
                },
                {
                    "player_id": "37",
                    "Buildings_name": "GHI"
                },
                {
                    "player_id": "38",
                    "Buildings_name": "JKL"
                }
            ]
        }

Upvotes: 2

Views: 71

Answers (2)

Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61409

Here you go, this go for each player and check if there are buildings and will map them to new structure. This will not filter values for buildings that do not have mapping to players, and will not include the buildings with no players.

var x = {
    "Players": [
        {
            "id": "35",
            "building_id": "8",
            "room_num": "101",
        },
        {
            "id": "36",
            "building_id": "9",
            "room_num": "102",
        },
        {
            "id": "37",
            "building_id": "10",
            "room_num": "103",
        },
        {
            "id": "38",
            "building_id": "11",
            "room_num": "104",
        }
    ],
    "Buildings": [
        {
            "id": "8",
            "name": "ABC"
        },
        {
            "id": "9",
            "name": "DEF"
        },
        {
            "id": "10",
            "name": "GHI"
        },
        {
            "id": "11",
            "name": "JKL"
        }
    ]
};

var res = $.map(x.Players, function(item) {
    return {
        player_id: item.id,
        building_name: $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        }).length != 0 ? $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        })[0].name : undefined
    }
})

and if you want to filter values that do not have relationships e.g INNER join

var resInnerJoin = $.grep($.map(x.Players, function(item) {
    return {
        player_id: item.id,
        building_name: $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        }).length != 0 ? $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        })[0].name : undefined
    }
}), function(it) {
    return it.building_name != undefined
})

Upvotes: 3

fdehanne
fdehanne

Reputation: 1718

If you need it in PHP :

$json = '{...}';

// create and PHP array with you json data.
$array = json_decode($json, true);

// make an array with buildings informations and with building id as key
$buildings = array();
foreach( $array['Buildings'] as $b ) $buildings[$b['id']] = $b;


$informations = array();

for ( $i = 0 ; $i < count($array['Players']) ; $i++ )
{
    $informations[$i] = array(
        'player_id' => $array['Players'][$i]['id'],
        'Buildings_name' => $buildings[$array['Players'][$i]['building_id']]['name']
    );
}

$informations = json_encode($informations);

var_dump($informations);

Upvotes: 1

Related Questions