-2
JSON=new Object();JSON.stringify=function(e){var e=e;var c={}.hasOwnProperty?true:false;var d=function(i){return i<10?"0"+i:i};var a={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};function b(i){if(/["\\\x00-\x1f]/.test(i)){return'"'+i.replace(/([\x00-\x1f\\"])/g,function(k,j){var l=a[j];if(l){return l}return j})+'"'}return'"'+i+'"'}function f(q){var m=["["],j,p,k=q.length,n;for(p=0;p<k;p+=1){n=q[p];switch(typeof n){case"undefined":case"function":case"unknown":break;default:if(j){m.push(",")}m.push(n==null?"null":JSON.parse(n));j=true}}m.push("]");return m.join("")}function h(i){return'"'+i.getFullYear()+"-"+d(i.getMonth()+1)+"-"+d(i.getDate())+"T"+d(i.getHours())+":"+d(i.getMinutes())+":"+d(i.getSeconds())+'"'}function g(n){if(typeof n=="undefined"||n===null){return"null"}else{if(n instanceof Date){return h(n)}else{if(typeof n=="string"){return b(n)}else{if(typeof n=="number"){return isFinite(n)?String(n):"null"}else{if(typeof n=="boolean"){return String(n)}else{if(n instanceof Array){return f(n)}else{var k=["{"],j,m,l;for(m in n){l=n[m];switch(typeof l){case"undefined":case"function":case"unknown":break;default:if(j){k.push(",")}k.push(g(m),":",l===null?"null":g(l));j=true}}k.push("}");return k.join("")}}}}}}}return g(e)};JSON.parse=function(json){return eval("("+json+")")};

        var myUserObject = {
                    "name": "John Doe",
                    "company": "Oracle",
                    "twitter" : "@johndoe",      
                  };


        myNewObject['users']=[];
        myNewObject['users'].push(myUserObject);

    alert(myNewObject.users[0].name)



var myFile = new File ("~/Desktop/test.json");

    if(myFile.open("w")){
        myFile.encoding = "UTF-8";
        myFile.write(JSON.stringify(myNewObject, undefined, "\r\n"));
        myFile.close();

        }

I am trying to add an object into an array of objects and stringify the object. When I push one object into another I can access the data correctly via an alert but when I write the object to a file via stringify the file is empty.

bhamlefty24
  • 9
  • 1
  • 4
  • 3
    `JSON=new Object();` is going to overwrite the native `JSON` object and the next command `JSON.stringify()`, will fail because a new Object doesn't have a `stringify()` method. Why are you doing this? – Scott Marcus Apr 04 '18 at 18:42
  • If I change : myFile.write(JSON.stringify(myNewObject, undefined, "\r\n")); To this: myFile.write(JSON.stringify(myUserObject, undefined, "\r\n")); The object is correctly stringified. Its only when I try and push an object inside an array that I get an error on main – bhamlefty24 Apr 04 '18 at 18:48
  • You should not overwrite the object.. :S Try using the native one ^^ – Helpha Apr 04 '18 at 18:49

1 Answers1

2

Your understanding of how to use JSON in particular and how to reference objects (in general) is the issue here.

Your new Object should not be assigned to JSON because JSON is a native object. You doing this wipes out that object reference.

Even if you didn't wipe out your reference to the native JSON object, JSON.stringify is a method and should have what you want stringified passed as an argument to it. You don't assign a function to it because that will wipe out the function already stored there.

console.log(JSON.stringify);  // The native object is ready to go
JSON = new Object();          // This wipes out your reference to the object
console.log(JSON.stringify);  // Now, the method doesn't exist!

Just assign the object you need stringified to a variable and pass that to JSON.stringify():

var myNewObject = {key1:true, key2:17, key3: [
  {k1: 10, k2:true, k3: "test" },
  {k1: 11, k2:true, k3: "testing" },
  {k1: false, k2:42, k3: "foo" },
]}; // Your object that needs stringification

var stringifiedObject = JSON.stringify(myNewObject);   // Stringify it!
console.log(stringifiedObject);                        // Done!
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Thanks for the response Scott. I have a quick follow up question, if I wanted to have key3: be an array of multiple objects how would I get that to stringify? – bhamlefty24 Apr 04 '18 at 21:57
  • @bhamlefty24 You don't do anything special at all. Just pass the the main object to `.stringify()` and it will be completely stringified. I'll update the answer to show this. – Scott Marcus Apr 04 '18 at 22:44