0

Updated to try to be more clear given the comments (Thank you for comments)

I apologize in advance for this question. I may just not have the vocabulary to properly research it. If I have an array of objects called restaurants stored in my project, for example: restaurants: [ {
"name": "joes's pizza", "url": "joespizza.com", "social properties": { "Facebook":"fb.com/pizza", "Instagram":"instagram.com/pizza", "googlePlus":"pizza+", "Twitter":"twitter.com/pizza" } }, {
"name": "tony's subs", "url": "tonys.com", "social properties": { "Facebook":"fb.com/subs", "Instagram":"instagram.com/subs", "googlePlus":"subs+", "Twitter":"twitter.com/subs" } }, {....} ]

I then run a function to add a unique idea to all the objects in the array. The result of console.log(restaurants) is this:

     {  
    "id": 3472, 
    "name": "joes's pizza",
    "url": "joespizza.com",
    "social properties": {
            "Facebook":"fb.com/pizza",
            "Instagram":"instagram.com/pizza",
            "googlePlus":"pizza+",
            "Twitter":"twitter.com/pizza"
        }
    },
     {   
    "id": 9987, 
    "name": "tony's subs",
    "url": "tonys.com",
    "social properties": {
            "Facebook":"fb.com/subs",
            "Instagram":"instagram.com/subs",
            "googlePlus":"subs+",
            "Twitter":"twitter.com/subs"
        }
    },
    {....}
]

I would now like to have this updated array of objects available to look at in my project, via the text editor, as a variable or restaurants.json file. How do I actually see the new modified json array and how do i save it so that i can work with it the same way i did this one above? I am currently doing this in the browser. I can see the results if i log it to the console but I need to be able to work with the new output. Thanks for taking the time to answer.

armand
  • 693
  • 9
  • 29
  • I don't understand your question. If you log the new output, you're already working with the modified object. – manonthemat Feb 09 '16 at 17:48
  • if you can see the results in the console, then just append them to an html element on your page instead of logging to the console. Example [here](https://jsfiddle.net/o8u9jtmf/) – devlin carnate Feb 09 '16 at 17:49
  • "Save" where? *"I can see the results if i log it to the console but I need to be able to work with the new output."* Uh? If you mutated the object (e.g. `obj.newProperty = 42;`) then you can just continue working with `obj`. – Felix Kling Feb 09 '16 at 17:55
  • Felix, i understand that i can work with it but i am not looking at it. Its kinda conceptual. Its just "return obj" where as the array i was modifying was a json file with a bunch of objects i could search through. If run a function removing every other object, how do i see the new array with objects removed: ie. just the results. – armand Feb 10 '16 at 09:47

2 Answers2

0

You can encode/decode JSON with JSON.stringify() and JSON.parse().

Aside from converting to/from JSON, you work with standard JS objects and arrays:

var array = JSON.parse(json_str);
array[0].address = "5th Avenue";
console.log(JSON.stringify(array));
zlumer
  • 6,844
  • 1
  • 25
  • 25
0

Well, there's really not enough information in your question but I assume a few things:

  1. You've loaded the json data from somewhere and it has been turned into a javascript object.
  2. You've edited the object somehow and wish to convert it back to json and save the changes.

Assuming the above to be true, you just need to serialize the object back to json and submit it back to your server where you can save it in any manner you deem appropriate.

  1. You can serialize the javascript object with JSON.stringify() (see https://stackoverflow.com/a/912247/4424504)
  2. Add the serialized json to a hidden field on the form and submit it.
  3. On the server when processing the form submission, grab the data from the hidden field and do with it what you wish.
  4. Or get it back to the server any way you wish (ajax call, whatever) the key point is to serialize the object to a json string and save it

Hope that helps...

Community
  • 1
  • 1
K. Meke
  • 433
  • 2
  • 14
  • Right now i am doing this on my local machine in the browser. Do i need to have a local server running to do what i want to do? – armand Feb 10 '16 at 11:13