3

For the following Google JSON-LD structure ...

<script type="application/ld+json" id="datablock-1">
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "honorificPrefix": "Dr",
  "givenName": "Albert",
  "familyName": "Einstein",
  "honorificSuffix": "PhD",
  "jobTitle": "Professor of Physics",
  "worksFor": [ {
      "@type": "EducationalOrganization",
      "department": "School of Science",
      "parentOrganization": "Princeton University",
      "address": [ {
          "@type": "PostalAddress",
          "streetAddress": "One Relativity Way",
          "addressCountry": "USA" } ]
    } ]
}
</script>

this HTML + JavaScript works:

<body>
    <dl><dt>Big Ideas</dt><dd id="dd-1"></dd></dl>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    var data = $("#datablock-1").html();
    var json = JSON.parse(data);
    var people = json["worksFor"];
    for (i=0; i<=people.length; i++) {
        var a=json["givenName"];
        var b=json["familyName"];
        var c=json["honorificSuffix"];
        var d=json["worksFor"][i]["parentOrganization"];
        $('#dd-1').append("<dd>"+a+" "+b+", "+c+". "+" "+d+"."+" "+"</dd>");
    }
</script>
</body>

Now I need to reach one level deeper in the object and add information about "@type": "PostalAddress".

But this HTML + JavaScript does not work:

<body>
    <dl><dt>Big Ideas</dt><dd id="dd-1"></dd></dl>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    var data = $("#datablock-1").html();
    var json = JSON.parse(data);
    var people = json["worksFor"]["address"];
    for (i=0; i<=people.length; i++) {
        var a=json["givenName"];
        var b=json["familyName"];
        var c=json["honorificSuffix"];
        var d=json["worksFor"][i]["parentOrganization"];
        var e=json["worksFor"]["address"][i]["streetAddress"];
        $('#dd-1').append("<dd>"+a+" "+b+", "+c+". "+" "+d+"."+" "+e+"</dd>");
    }
</script>
</body>

What am I doing wrong? How to correct the error?

3
  • Which @type object and what value? Commented Oct 24, 2015 at 10:17
  • @AdamJeffers "@type": "PostalAddress" and key "address" and value "streetAddress" Commented Oct 24, 2015 at 10:20
  • You are trying to access "address" from "worksFor". You need another loop on "worksfor" since its an array. Commented Oct 24, 2015 at 10:21

3 Answers 3

2

Wouldn't you just hop into postal address from the top, without changing the condition? I've changed your people variable name to worksFor since that is what you are iterating through, since it doesn't look like you have a list of people atm. Maybe I'm wrong?

<body>
    <dl><dt>Big Ideas</dt><dd id="dd-1"></dd></dl>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    var data = $("#datablock-1").html();
    var json = JSON.parse(data);
    var worksFor= json["worksFor"];
    for (i=0; i<=worksFor.length; i++) {
        var a=json["givenName"];
        var b=json["familyName"];
        var c=json["honorificSuffix"];
        var d=json["worksFor"][i]["parentOrganization"];
        var e=json["worksFor"][i]["address"][0]["streetAddress"];//this line is changed
        $('#dd-1').append("<dd>"+a+" "+b+", "+c+". "+" "+d+"."+e+"</dd>");
    }
</script>
</body>
Sign up to request clarification or add additional context in comments.

6 Comments

Does your solution work for you? It's not working on my end.
I missed that address is an array, that might work now.
OK, got it working, but please update your answer as follows. Change postalAddress to streetAddress and modify the append to "+d+"."+e+"</dd>"
Also, please explain why you changed people to worksFor. Reason is that I have other classes to add at the same level as worksFor in the JSON-LD structure. Examples are alumniOf and sameAs. I used people as the parent for those keys.
So I could modify your code easier, if people is the correct name, by all means flip it back.
|
2

This line fails:

people = json["worksFor"]["address"]

And you end up people as undefined, reason being json["worksFor"] gives you array.

1 Comment

Would you show me the proper way to compose this? I'll accept the working answer.
1

This works for me fine...

var json  = {
  "@context": "http://schema.org/",
  "@type": "Person",
  "honorificPrefix": "Dr",
  "givenName": "Albert",
  "familyName": "Einstein",
  "honorificSuffix": "PhD",
  "jobTitle": "Professor of Physics",
  "worksFor": [ {
      "@type": "EducationalOrganization",
      "department": "School of Science",
      "parentOrganization": "Princeton University",
      "address": [ {
          "@type": "PostalAddress",
          "streetAddress": "One Relativity Way",
          "addressCountry": "USA" } ]
    } ]
}


json = JSON.parse(json);

json.worksFor[0].address[0]["@type"].PostalAddress.address = 'streetAddress'

1 Comment

Correctomundo. But the issue is that the solution has to work with a Google JSON-LD structure. The JSON-LD is a separate structure that is then formatted by JavaScript processing. To say it another way, your solution is not valid on Google Structured Data Testing Tool.

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.