cancel
Showing results for 
Search instead for 
Did you mean: 

add item in json file

Former Member
0 Kudos

Hi experts,

i need some help :

i have an list-detail app in sapui5 made in eclipse and i have  a json file called item.json in a folder called json in the web content,

like this

{

"data":[{"name":"james","last name":"ruler"},

           {"name":"joan","last name":"fighter"}    ]}   etc...

this data i have in a list the list shows a detail in other page and i have a 3rd page that has a form in blank

when i full or complete the form i need to use a button to add one more item to my json file in the folder i have listen than this can be do with a filewriter

or can you tell me how can i do this please!!

Regards,

Simón.

Accepted Solutions (1)

Accepted Solutions (1)

yoppie_ariesthio
Participant
0 Kudos

var yourDataModel = {

                                          "data":[

                                                         {"name":"james","last name":"ruler"},

                                                         {"name":"joan","last name":"fighter"}

                                                      ]

                                      }


If you want to add data to your model and update your content list you can simply use Array.push();

I think you can't just rewrite your local .json file because it's on client side and restricted for security reason.


yourDataModel.data.push({"name":"james","last name":"ruler"});

Best Regards,

Yoppie A.

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Yoppie,

your answer help me to do what i want but i want to save the changes in a local file in  my pc only for testing for show my boss that the aplication really works can be ths done with a filewriter or append?

Thanks for your help.

Regards,

Simón

yoppie_ariesthio
Participant
0 Kudos

Hi Simon,

please refer to this http://www.html5rocks.com/en/tutorials/file/filesystem/

Regards

amolgupta
Active Contributor
0 Kudos

Hi Simon,

You can easily add one calculated field to your service in JSON format.

// CODE SAMPLE STARTS HERE

var service = {};

service.persons = [{name:"Amol", age:"31"}, {name:"Tom", age:"50"}];

/* at this point :

service = {"persons":[{"name":"Amol","age":"31"},{"name":"Tom","age":"50"}]}

can be checked with :

console.log(JSON.stringify(service));

*/

service.persons[0].city = "London";

service.persons[1].city = "Berlin";

/* at this point

service = {"persons":[{"name":"Amol","age":"31","city":"London"},{"name":"Tom","age":"50","city":"Berlin"}]}

can be checked with :

console.log(JSON.stringify(service));

*/

// CODE SAMPLE ENDS HERE

You can not change source code in your designtime with runtime

To make changes to the .json file in Eclipse you have to do it in your design time in Eclipse. You can not change design time artifacts (source code on your computer) from runtime i.e. running apps.

Potential Solution : PERSISTENCE

1). You can store the JSON in some persistence like a table

2). Browsers can store data too in caches.

I hope this helps !

Cheers,

Amol G.



Former Member
0 Kudos

Hi Amol,

thank you for your help but what i want to add is a complete line to the json array not a field,

like this:

{data :[{"name":"xxx","last name":"xxx"},{"name":"xxx","last name":"xxx"}]}

i already has this data in 1 page showing in a list and have another page with the  details of the items like last name then i have a 3rd page where i have a form when i populate the form i want to press a button and add one more line to the array like this

{data :[{"name":"xxx","last name":"xxx"},{"name":"xxx","last name":"xxx"},{"name":"xxx","last name":"xxx"}]}

for showing in the list of the 1rst page. i hope you understand me.

Regards.

Simón

amolgupta
Active Contributor
0 Kudos

Hi Simon,

This time I got you right and have a solution too !

var service = {};

service.persons = [{name:"Amol", age:"31"}, {name:"Tom", age:"50"}];

/* at this point :

service = {"persons":[{"name":"Amol","age":"31"},{"name":"Tom","age":"50"}]}

can be checked with :

console.log(JSON.stringify(service));

*/

service.persons[service.persons.length] = {};

service.persons[service.persons.length-1].name = "Nelly";

service.persons[service.persons.length-1].age = "40";

/* at this point

service = {"persons":[{"name":"Amol","age":"31"},{"name":"Tom","age":"50"},{"name":"Nelly","age":"40"}]}

You can check it with :

console.log(JSON.stringify(service));

*/


I hope this helps !


Cheers,

Amol G.

Former Member
0 Kudos

Hi Amol

sorry but i'm still not understand what i really need to do is an append or filewriter to a sistem file from my form.

amolgupta
Active Contributor
0 Kudos

Hi Simon,
I guess you wish to write to server file system with SAPUI5 javascript. You might find the below quoted text relevant. Client side Java Script usually does not allow access to server file system as this might lead to security breaches / hackers attempting code injection to try to gain access to data on server.

http://stackoverflow.com/questions/29898158/read-write-txt-file-on-server-using-only-javascript-with...

"Only if JavaScript is your server side language (e.g. with Node.JS).
Servers don't let you write files to them by default. That would be a horrible security problem."

Recommended Potential Solution as recommended before PERSISTENCE

Store your data in a table.

1). If in ABAP stack, expose as service via SAP Gateway.

2). If in Java Stack, expose as service via JPA/Apache Olingo

3). If in HANA environment, expose as service via XSJS

I hope this answers your query.

Cheers,
Amol G.

saivellanki
Active Contributor
0 Kudos

Hi Simon,

Create a new object, pass those form values to object and push that new object to JSON. 

Will this sample help? Plunker

In above sample, enter the values in form and click on submit.

Regards,

Sai.

Former Member
0 Kudos

Hi sai,

in your sample you add data to the form i need through the form add item in to a json file that is in a folder in my web content of the app. thank you

regards

simón