Help Parsing JSON data

Hi All

I’m running this function on a button click that returns JSON data

          window.scoreView.getScore().done(function(scoreData) {
// do something with the returned score data

 hypeDocument.getElementById("demo").innerHTML = JSON.stringify(scoreData, null, "  ");
});

I’m currently just writing the JSON data to an element. I don’t need this functionality, I’m just doing it to show the data.

Which for my example (see attached file) returns the following JSON data:

{ "staves": [ { "measures": [ { "noteSets": [ { "position": 0, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 60, "step": 0, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1, "duration": 1, "voice": 0, "beam": false, "notes": [] }, { "position": 2, "duration": 2, "voice": 0, "beam": false, "notes": [] } ] } ] } ] }

I’d like to be able to do a couple of things with the “pitch” data:

  1. If the pitch is evaluated as something other than 60 I’d like to run a timeline or some other javascript.

…and 2.) If the pitch is a specific number like say 62, I’d like to be able to run a timeline or some other document.

I’ve spent a ton of time on this just getting this far so anyone that can help me get over the finish line that would be so, so awesome. Thanks in advance!

-Matt

checkNote_MM_v1.hype.zip (16.3 KB)

Well they couldn’t of made that more a pain. Objects within arrays within Objects…

scoreData.staves[0].measures[0].noteSets[0].notes[0].pitch)

You do not need the JSON.stringify

var pitch = scoreData.staves[0].measures[0].noteSets[0].notes[0].pitch;
  
  
  hypeDocument.getElementById("demo").innerHTML = pitch;
1 Like

sometimes a jslibrary is nice to handle complex jsondata.

in your case for jsonQ (11kb weight) it’ll be sthg like

var _pitches = jsonQ(yourJson).find('pitch') //return -> array; pitches[0].value() //eg = first value for pitch

2 Likes

Thanks Mark and Hans. This is so helpful!

I sometimes need to find the value of two or more of the objects for example I want to evaluate a note in a certain ‘stave’ in a certain ‘measure.’

Would Han’s library be recommended or is there a more basic way to do this?

For example, how would I target the second note in the second stave in the second measure of this:

{ "staves": [ { "measures": [ { "noteSets": [ { "position": 0, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 55, "step": 4, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1, "duration": 0.5, "voice": 0, "beam": true, "notes": [ { "pitch": 57, "step": 5, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1.5, "duration": 0.5, "voice": 0, "beam": false, "notes": [ { "pitch": 59, "step": 6, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 2, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 60, "step": 0, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 3, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 64, "step": 2, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] } ] }, { "noteSets": [ { "position": 0, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 62, "step": 1, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 59, "step": 6, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 2, "duration": 2, "voice": 0, "beam": false, "notes": [ { "pitch": 60, "step": 0, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] } ] } ] }, { "measures": [ { "noteSets": [ { "position": 0, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 53, "step": 3, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1, "duration": 0.5, "voice": 0, "beam": true, "notes": [ { "pitch": 55, "step": 4, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1.5, "duration": 0.5, "voice": 0, "beam": false, "notes": [ { "pitch": 57, "step": 5, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 2, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 59, "step": 6, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 3, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 62, "step": 1, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] } ] }, { "noteSets": [ { "position": 0, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 60, "step": 0, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 1, "duration": 1, "voice": 0, "beam": false, "notes": [ { "pitch": 57, "step": 5, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] }, { "position": 2, "duration": 2, "voice": 0, "beam": false, "notes": [ { "pitch": 59, "step": 6, "alteration": 0, "accidental": null, "noteHead": null, "tied": false } ] } ] } ] } ] }Check Note

Would I edit Mark’s code somehow:
var pitch = scoreData.staves[0].measures[0].noteSets[0].notes[0].pitch;

Where the stave array is 0 for first stave and 1 for second stave etc.
I believe the array for measure would be the same, 0 for measure 1 and 1 for measure 2.

“position” is the offset for each measure so position: 1 would target the second note.

grrr… I am beginning to hate this data.

var pitch = scoreData.staves[1].measures[1].noteSets[1].notes[0].pitch

Use the Console to log the data and you will then be able to see what you need.

2 Likes

Thank you so much Mark! This is tremendously helpful!

I have one more hurdle to get over and then I’ll share the project I’m working on.

1 Like

I’ve researched Switch statements and I’m sure what I’m trying to do is wrong. Not only that but it is not yielding the results I need. I’ve looked up several examples on Stackoverflow and think that maybe I should be using an if…else statement?

	          window.scoreView.getScore().done(function(scoreData) {
    // do something with the returned score data
    
   var pitch = scoreData.staves[1].measures[0].noteSets[0].notes[0].step
    
    
   var pitch2 = scoreData.staves[1].measures[0].noteSets[3].notes[0].step
   
   
   var pitch3 = scoreData.staves[1].measures[1].noteSets[0].notes[0].step
   
   var pitch4 = scoreData.staves[1].measures[1].noteSets[2].notes[0].step
    
   
  
    
    //evaluate 1st entry
        switch(true) {
     case (pitch == "2"):        
     case (pitch == "6"):
     
       hypeDocument.startTimelineNamed('move', hypeDocument.kDirectionForward)
         break; 
         
     
        default:
        hypeDocument.startTimelineNamed('1stentrywrong', hypeDocument.kDirectionForward);
      return;

    }
    
    
    //evaluate 2nd entry 
        switch(true) {
     case (pitch2 == "0"):   
     case (pitch2 == "4"):
       hypeDocument.startTimelineNamed('move', hypeDocument.kDirectionForward)
         break; 
     
            
        default:
        hypeDocument.startTimelineNamed('2ndentrywrong', hypeDocument.kDirectionForward);
       return;

    }
    
    //evalutate 3rd entry
       switch(true) {
     case (pitch3 == "6"):
     case (pitch3 == "3"):
       hypeDocument.startTimelineNamed('move', hypeDocument.kDirectionForward)
         break; 
     
            
        default:
        hypeDocument.startTimelineNamed('3rdentrywrong', hypeDocument.kDirectionForward);
       return;

    }
    
    //evaluate 4th entry
       switch(true) {
     case (pitch4 == "2"):
     case (pitch4 == "5"):
       hypeDocument.startTimelineNamed('move', hypeDocument.kDirectionForward)
         break; 
     
            
        default:
        hypeDocument.startTimelineNamed('4thentrywrong', hypeDocument.kDirectionForward);
       return;

    }  
    
     console.log(scoreData);
    
    
   
  });
  
  score3.focus();

checkNote_MM_v2.hype.zip (25.4 KB)

I figured it out! Is this the way you might do it @MarkHunte ?

 //evaluate all entries
    switch(true) {
 case ((pitch == "2" || pitch == "6") && (pitch2 == "0" || pitch2 == "4") && (pitch3 == "6" || pitch3 =="3") && (pitch4 == "2" || pitch4 == "5")):        
 
 
   hypeDocument.startTimelineNamed('move', hypeDocument.kDirectionForward)
     break; 
     
 case ((pitch != "2" || pitch != "6") && (pitch2 == "0" || pitch2 == "4") && (pitch3 == "6" || pitch3 =="3") && (pitch4 == "2" || pitch4 == "5")):        
 
 
   hypeDocument.startTimelineNamed('1stentrywrong', hypeDocument.kDirectionForward)
     break; 
     
  case ((pitch == "2" || pitch == "6") && (pitch2 != "0" || pitch2 != "4") && (pitch3 == "6" || pitch3 =="3") && (pitch4 == "2" || pitch4 == "5")):        
 
 
   hypeDocument.startTimelineNamed('2ndentrywrong', hypeDocument.kDirectionForward)
     break;
     
   case ((pitch == "2" || pitch == "6") && (pitch2 == "0" || pitch2 == "4") && (pitch3 != "6" || pitch3 !="3") && (pitch4 == "2" || pitch4 == "5")):        
 
 
   hypeDocument.startTimelineNamed('3rdentrywrong', hypeDocument.kDirectionForward)
     break;
     
    case ((pitch == "2" || pitch == "6") && (pitch2 == "0" || pitch2 == "4") && (pitch3 == "6" || pitch3 =="3") && (pitch4 != "2" || pitch4 != "5")):        
 
 
   hypeDocument.startTimelineNamed('4thentrywrong', hypeDocument.kDirectionForward)
     break;          
     
 
    default:
   
   return;

}