Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity › Forums › Adobe After Effects › Trying to Parse nested JSON file in After Effects scripting

  • Trying to Parse nested JSON file in After Effects scripting

    Posted by Brian Williams on September 21, 2020 at 5:52 pm

    can someone show me what I am doing wrong here? I have a json file that I am parsing through a script in After Effects, and I’m not sure how to iterate through the section of the json file.

    My json file is:

    {
        "Timestamp": "20-Sep-2020 12:38:07.177",
        "m11": 0.2202,
        "m12": 0.8858,
        "m13": 0.4085,
        "m21": -0.9708,
        "m22": 0.2399,
        "m23": 0.0031,
        "m31": -0.0952,
        "m32": -0.3972,
        "m33": 0.9128,
        "qX": -0.13,
        "qY": 0.1635,
        "qZ": -0.6026,
        "qW": -0.7702
      },
      {
        "Timestamp": "20-Sep-2020 12:38:07.186",
        "m11": 0.2204,
        "m12": 0.8861,
        "m13": 0.4078,
        "m21": -0.9708,
        "m22": 0.2401,
        "m23": 0.0031,
        "m31": -0.0952,
        "m32": -0.3965,
        "m33": 0.9131,
        "qX": -0.1297,
        "qY": 0.1632,
        "qZ": -0.6026,
        "qW": -0.7703
      }

    And my jsx file is this:

    #include "json2.js"
    
    var file = new File;
    var check = 0;
    
    var mainWindow = new Window("palette", "File Reader", undefined);
    mainWindow.orientatizzaaq1on = "column";
    
    var groupOne = mainWindow.add("group", undefined, "groupOne");
    groupOne.orientation = "row";
    var fileLocBox = groupOne.add("edittext", undefined, "Selected File Location");
    fileLocBox.size = [150, 20];
    var getFileButton = groupOne.add("button", undefined, "File...");
    getFileButton.helpTip = "Select a .json file to change the comp";
    
    var groupTwo = mainWindow.add("group", undefined, "groupTwo");
    groupTwo.orientation = "row";
    var applyButton = groupTwo.add("button", undefined, "Apply");
    
    mainWindow.center();
    mainWindow.show();
    
    getFileButton.onClick = function() {
        file = file.openDlg("Open a file", "Acceptable Files:*.json");
        fileLocBox.text = file.fsName;
        check = 1;
        }
    
    applyButton.onClick = function() {
            if(check == 0) {
                    alert("Please select a file");
                    return false;
                } else {
                    //app.beginUndoGroup("Comp Changes");
                    var fileExtension = fileLocBox.text;
                    var fileData;
                    
                    if(fileExtension.substring(fileExtension.length-4, fileExtension.length) == "json") {
                        fileData = readJson();
                        
                        changeComp(fileData);
                    }
                alert("done");
       }
    }
    function readJson() {
        var currentLine;
        var jsonStuff = [];
        file.open("r");
            while(!file.eof) {
                    currentLine = file.readln();
                    jsonStuff.push(currentLine);
                }
            file.close();
            jsonStuff = jsonStuff.join("");
            var parsedJson = JSON.parse(jsonStuff);
            
            return parsedJson;
        }
    
    function changeComp(data) {
        var nullz = app.project.activeItem.layers.addNull();
        nullz.threeDLayer = true;
        nullz.name = "parent to me";
        var size = Object.keys(data).length;
        for (i = 0; i < size; i++) {
            var temp =data[1].m11; 
            var t =i/25;
           nullz.xRotation.setValueAtTime(t, temp);
          }
      }

    I have a feeling my json file may need to be formatted differently, this is just how it came to me from another app.

    So the idea is it will go through the json file, get the “m11” value, apply it to the null’s rotation, then go on to the next frame, grab the next “m11” and so on… In my script, if I just say:

    var temp =data.m11; 
    var t =i/25;
    nullz.xRotation.setValueAtTime(t, temp);

    It works and will grab the value of the first “m11” and apply it to the null object. But if I try to get to the second “m11” by saying

    var temp =data[1].m11;

    or

    var temp =data.m11[1];

    it doesn’t work, so just trying to figure out what I am doing wrong? Is it the way the json is formatted? The json originally came with [] at the end and beginning, but leaving those in would cause the script to crash when it tried to read the file, taking them out make it somewhat work, as I mentioned above.

    Or is it the script? Or both, I’m guessing?

    Brian Williams replied 6 years ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy