0

I'm trying to convert a JavaScript object to a json.

Practically I have created a website where you can upload a json, edit it and download it with your changes.

For convenience when I edit it creates a javascript object that will be converted at the end of the changes with JSON.stringify.

The problem is that the final Json should have the same characteristics as the initial one with the elements associated to the different key.

For example the first one can be this:

"tickets": {
    "use": "Valida",
    "useagain": "Valida di nuovo",
    "usetitle": "Convalida biglietto",
    "usemessage": "Vuoi convalidare il biglietto ora?",
    "purchaseconfirmtitle": "Confermi l\"acquisto?",
    "purchaseconfirmmessage": "Potrai convalidare il biglietto più tardi",
    "minutes": "Minuti",
}

The result must be like this:

"tickets": {
    "use": "Example",
    "useagain": "Example1",
    "usetitle": "Example2",
    "usemessage": "Example3,
    "purchaseconfirmtitle": "Example4",
    "purchaseconfirmmessage": "5",
    "minutes": "Minuti",
}

How do I make sure that all elements will be children of "ticket" (for example)?

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
Mister98
  • 69
  • 1
  • 8

3 Answers3

0

You are looking for JSON.stringify().

Naqash Malik
  • 1,707
  • 1
  • 12
  • 14
  • i don't know why but your link don't appear https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – AlainIb May 11 '16 at 07:42
  • Its no big deal. [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) – Naqash Malik May 11 '16 at 07:45
  • nope but in first look i didn't get it's a link. i think you trying to get the second element of `[JSON.stringify()]` ;) – AlainIb May 11 '16 at 07:47
0

According to W3Schools(consider following example),

var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

Here is your object,

var obj = JSON.parse(text);

Here is the link.

Hasitha
  • 558
  • 1
  • 12
  • 26
0

I think you want to compare two javascript object.

Try this url

http://procbits.com/2012/01/19/comparing-two-javascript-objects

is that what you are looking for?

Jitendra Tiwari
  • 1,651
  • 3
  • 14
  • 28