0

I've got a JSON looking like this (design might be wrong)

{ "Supermarkt": { "name": "Supermarkt", "translations": { "trolley": "Einkaufswagen", "vegetables": "Gemüse", "cocoa": "Kakao", "chocolate": "Schokolade" }, "pronunciations": [] }, "Script1336Kidee": { "name": "Script1336Kidee", "translations": { "Trojaner": "RAT", "Laufzeit-Packer": "Magie", "PHP": "Der letzte Dreck", "JavaScript": "Wild-West" }, "pronunciations": [] } } 

Which I get with a ajax call (already JSON-decoded through dataType: "json" $.ajax option):

let lessonCall = $.ajax("https://www2.htw-dresden.de/~s70357/vokabel.php/",{dataType: "json"}); lessonCall.fail((jqXHR, status, error) => { console.log(status); console.log(error); }); lessonCall.done((data,status) => { console.log(status); console.log(data); for (let lesson in data){ console.log(lesson); console.log(lesson.name); } }); 

Problem is lesson.name is undefined although console.log(data); show the healthy data Object with the healthy subsub-Objects, but lesson seems to be just a string-like thing.

How can I iterate through my "name"s?!

4
  • lesson is the property name not the value, per this Commented Jun 7, 2017 at 19:55
  • @James So how can I fix it? Commented Jun 7, 2017 at 19:58
  • 1
    How do you get a property value from an object, having the property name in a variable? obj[propertyName]. Commented Jun 7, 2017 at 20:11
  • Yeah, I was not realizing that I'm operating at that "array-level" I was trying lesson["name"] but it never occurred to that for in just get's me the indexes. Javascript beginner, sorry, but thanks! Commented Jun 7, 2017 at 20:16

2 Answers 2

1

Try this:

for (let i in data){ let lesson = data[i]; console.log(lesson); console.log(lesson.name); } 
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried calling let jsonData = JSON.parse(data) before your for loop?

Would you be able to modify the json format? If so, you could turn it into an array:

[ { "name": "Supermarkt", "translations": { "trolley": "Einkaufswagen", "vegetables": "Gemüse", "cocoa": "Kakao", "chocolate": "Schokolade" }, "pronunciations": [] }, { "name": "Script1336Kidee", "translations": { "Trojaner": "RAT", "Laufzeit-Packer": "Magie", "PHP": "Der letzte Dreck", "JavaScript": "Wild-West" }, "pronunciations": [] } ] 

5 Comments

Yes, then I get parse erros as expected since it's already parsed by $.ajax : Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) at Object.lessonCall.done (vokabeltrainer.js:31) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at A (jquery.min.js:4) at XMLHttpRequest.<anonymous>
What does the console.log(lesson); show?
Just Supermarkt and Script1336Kidee (just the property name not the value, as James pointed out) . Michał Perłakowski s Answer worked, I guess it's in one more array, I don't get that whole javascript "type system" yet. But thx for trying!
Cool. No problem! I also posted another json format you could try if you have the access to modify it since you mentioned "(design might be wrong)"
@Eriv B Funny, I had it in this format, but IIRC that gives you parse errors, since it's not legal json. (That whole thing is top level)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.