How to split Json file


#1

I need to separate the json file data
test.ts

   getDataEtalon() {
    return  this.data.getEtalon()
            .subscribe(
                  data=> {
                      this.etalon = data.split('##');
                      console.log(this.etalon);         
                  },
                  err=> console.log(err)
                  
            );
      }
{
0: "AKERMI##1988",
5: "AKIM DE DUCOR##2005",
6: "AL FATIH##2003",
9: "AMER##1984",
13: "BELAMER##2004",
15: "CHAHATA##1990",
20: "CHARH##1999",
23: "CHEIKH EL ARAB##1990",
24: "DAHESS##1999",
35: "DARIKE##1991",
44: "DARMAN##1998",
50: "DAYJUR##1991",
51: "DJARNI DES FORGES##1995",
55: "DJELMANE##1999",
77: "DJOURDAN##1999",
83: "DORMANE##1984",
}

EXCEPTION: data.split is not a function


#2

data is this JSON object? You can’t use split() on an object, only on strings. You have to iterate over your object and apply the function to your values.


#3

I try but it does not work

 getDataEtalon() {

    return  this.data.getEtalon()
            .subscribe(
                  data=> {
                    for ( var i = 0 ;  i <= data.length ; i++)
                          {
                         this.etalon = data[i].split('##');
                         }
                          console.log(data);
                         
                    }  );
      }

#4

Because it’s an object and not an array, keys are different.

for(let key in data) {
    ... data[key].split("##") ...
}

But FYI now you’re replacing this.etalon every time you loop through…


#5

works , but it only displays the last name


#6
   getDataEtalon() {
   
    return  this.data.getEtalon()
            .subscribe(
                  data=> {
                    for (let key in data)
                          {
                         this.etalon = data[key].split('##');
                         }
                          console.log(this.etalon);
                         
                    }  );
      }

#7

You need to provide some more information for us to be able to help you further - we have no idea what you’re trying to achieve exactly. What does your HTML look like?

Also, if this is how you indent your code in your project, I would really recommend doing it like this:

getDataEtalon() {
	return this.data.getEtalon().subscribe(
		data => {
			for (let key in data) {
				this.etalon = data[key].split('##');
			}
			console.log(this.etalon);
		}
	);
}

It really improves readability. Also, if this is an HTTP call you probably want to handle potential errors

getDataEtalon() {
	return this.data.getEtalon().subscribe(
		data => {
			for (let key in data) {
				this.etalon = data[key].split('##');
			}
			console.log(this.etalon);
		},
		err => {
			console.log(err);
		}
	);
}

#8

JSON File :

{
0: "AKERMI##1988",
5: "AKIM DE DUCOR##2005",
6: "AL FATIH##2003",
9: "AMER##1984",
13: "BELAMER##2004",
15: "CHAHATA##1990",
20: "CHARH##1999",
23: "CHEIKH EL ARAB##1990",
24: "DAHESS##1999",
35: "DARIKE##1991",
44: "DARMAN##1998",
50: "DAYJUR##1991",
51: "DJARNI DES FORGES##1995",
55: "DJELMANE##1999",
77: "DJOURDAN##1999",
83: "DORMANE##1984",
103: "FARADJALA##1993",
105: "GITPEN##1994",
110: "HAJJAM##1995",
175: "HAKIM DU BAC##1995",
180: "HALIM##1995",
185: "HAMID##1996",
194: "HAMZA##1995",
204: "HYRAM##1995",
206: "ISSAOUI##1996",
210: "JAMAK##1997",
213: "JEDEL##1997",
231: "JESROY DE CHAILLAC##1997",
232: "KAHLOUN##1998",
233: "KERBELLA##1992",
236: "LATHLETH##1999",
246: "MAJD AL ARAB##2002",
251: "MAKZAN##1991",
258: "MANGANELLO##1990",
259: "MANGUIER DE PIBOUL##1995",
263: "MAYSOUN##2000",
266: "MIGYESS##2000",
269: "MOUSSOUL##2000",
324: "MUNEEF##2001",
334: "NADIM##2001",
336: "NAKKACH##2001",
337: "NEDJAM LOTOIS##1996",
338: "NEMROD DU PAON##2001",
345: "NEZ D'OR##1997",
346: "NIZAM##1998",
348: "ORIENT EXPRESS##1995",
349: "OUAAD##2003",
350: "PHARAON DES CEDRES##2000",
351: "RAAD##2003",
357: "RAEEH##2003",
358: "RAFII##2003",
366: "ROUSHAAN##2003",
371: "SAMIR##1985",
391: "SARKI D'ESPIENS##1992",
392: "SAYAF##2004",
398: "SEMAOU'EL##2004",
400: "SOUR##1985",
404: "SULTAN AL BADR##2004",
405: "TIDJAM LOTOIS##1999",
433: "TOUWAYSSAN##1986",
434: "VENT DREDY##1992",
435: "VIOLET##2001",
436: "ZEIDOUN##1987"
}

output


#9