Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove Intial Nesting in JSON array with PHP

I have this JSON array starting with the nesting string "ELEVEURS" and I want to have it removed and should get the output as below. Could someone solve this please!

INPUT

 $json='{ "ELEVEURS" : 
       [{
        "CHEPTEL":"12001116",
        "NOM":"La Ferme de Jean-Marc et Aurélien ",
        "CODE_POSTAL":"12630                         ",
        "VILLE":"AGEN D AVEYRON",
        "LATITUDE":"44.343518",
        "LONGITUDE":"2.716004",
        "DESCRIPTIF_FERME":"",
        "DEMARCHE" : [
        {
        "DEMA_CODE":"08-93",
        "ANNEE_ADHESION":"2016",
        "RACE_MERES":"Limousine",
        "DESCRIPTIF_ATELIER":""
        }
        ]
        },
        {
        "CHEPTEL":"12004022",
        "NOM":"La Ferme du Broussier ",
        "CODE_POSTAL":"12300                         ",
        "VILLE":"ALMONT LES JUNIES",
        "LATITUDE":"44.592071",
        "LONGITUDE":"2.328516",
        "DESCRIPTIF_FERME":"",
        "DEMARCHE" : [
        {
        "DEMA_CODE":"08-93",
        "ANNEE_ADHESION":"2000",
        "RACE_MERES":"Limousine",
        "DESCRIPTIF_ATELIER":""
        }
        ]
        }'

OUTPUT

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$json='  [{
        "CHEPTEL":"12001116",
        "NOM":"La Ferme de Jean-Marc et Aurélien ",
        "CODE_POSTAL":"12630                         ",
        "VILLE":"AGEN D AVEYRON",
        "LATITUDE":"44.343518",
        "LONGITUDE":"2.716004",
        "DESCRIPTIF_FERME":"",
        "DEMARCHE" : [
        {
        "DEMA_CODE":"08-93",
        "ANNEE_ADHESION":"2016",
        "RACE_MERES":"Limousine",
        "DESCRIPTIF_ATELIER":""
        }
        ]
        },
        {
        "CHEPTEL":"12004022",
        "NOM":"La Ferme du Broussier ",
        "CODE_POSTAL":"12300                         ",
        "VILLE":"ALMONT LES JUNIES",
        "LATITUDE":"44.592071",
        "LONGITUDE":"2.328516",
        "DESCRIPTIF_FERME":"",
        "DEMARCHE" : [
        {
        "DEMA_CODE":"08-93",
        "ANNEE_ADHESION":"2000",
        "RACE_MERES":"Limousine",
        "DESCRIPTIF_ATELIER":""
        }
        ]'

I have this INPUT of JSON array starting with the nesting string "ELEVEURS" and I want to have it removed and should get the output as above. Could someone solve this please!

>Solution :

I think you have a syntax error in your first JSON. I’ve corrected it in this answer.

$json = '
{
     "ELEVEURS": [
          {
            "CHEPTEL":"12001116",
            "NOM":"La Ferme de Jean-Marc et Aurélien ",
            "CODE_POSTAL":"12630                         ",
            "VILLE":"AGEN D AVEYRON",
            "LATITUDE":"44.343518",
            "LONGITUDE":"2.716004",
            "DESCRIPTIF_FERME":"",
            "DEMARCHE" : [
                {
                "DEMA_CODE":"08-93",
                "ANNEE_ADHESION":"2016",
                "RACE_MERES":"Limousine",
                "DESCRIPTIF_ATELIER":""
                }
            ]
          },
          {
                "CHEPTEL":"12004022",
                "NOM":"La Ferme du Broussier ",
                "CODE_POSTAL":"12300                         ",
                "VILLE":"ALMONT LES JUNIES",
                "LATITUDE":"44.592071",
                "LONGITUDE":"2.328516",
                "DESCRIPTIF_FERME":"",
                "DEMARCHE" : [
                    {
                        "DEMA_CODE":"08-93",
                        "ANNEE_ADHESION":"2000",
                        "RACE_MERES":"Limousine",
                        "DESCRIPTIF_ATELIER":""
                    }
                ]
            }
       ]
}';


echo(json_encode(json_decode($json)->ELEVEURS, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); // done!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading