0

I have the following JSON response from a web service. I want to use it on the front end but the "codeSubstance" is not showing anything. How do I parse it correctly ?

I've tried the following :

<tr *ngFor ="let m of medicamentsDetailslist;">
  <td>{{m.compositions.substancesActives.codeSubstance}}</td>
</tr>

and this also

 <tr *ngFor ="let m of medicamentsDetailslist;">
      <td>{{m.compositions.substancesActives["codeSubstance"]}}</td>
    </tr>

Jsonparse

Any help appreciated

2 Answers 2

3

compositions and substancesActives are again an array, so you should be iterating over it again

<tr *ngFor ="let m of medicamentsDetailslist;">
     <td>
        <ng-container *ngFor="let comp of m.compositions">
           <ng-container *ngFor="let sub of comp.substancesActives">
              {{sub.codeSubstance}}
           </ng-container>
        </ng-container>
     </td>
</tr>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Amit. I have tried your code but it is still blank. The data is here. Console.log shows it. Where can be the issue ?
Sorry, I see that 'compositions' is also an array.. You will need another iteration over ther
Thanks again. Indians smart people :)
I recommend to have a strong type structure, after getting assigned your json to an object on you Observable , you could do simply only one ngFor against the right structure, ngFor is expensive for large amount of data
1

hi i think you have an array in substancesActives, so try this :

<tr *ngFor ="let m of medicamentsDetailslist;">
  <td>{{m.compositions.substancesActives[0].codeSubstance}}</td>
</tr>

If it's ok, so add a new loop for substanceActives

EDIT (more details in comments) :

m.compositions[0].substancesActives[0].codeSubstance

4 Comments

Tried it too but got that : Cannot read property '0' of undefined
Hum... And the code of Amit Chigadani doesn't work too ? And are you sure, codeSubstance can't be null ?
Here it is the good one but it was near : <td>{{m.compositions[0].substancesActives[0].codeSubstance}}</td>
Yes, i ve just see the problem ! Nice ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.