How to create properties from variables in JSON

JSON and JavaScript loves each other… interact between them is something completely natural and you can create an object dynamically from scratch.

Some times we need create a JSON and his properties from variables and not from fixed values, this properties can be used at future as some kind of index and will be a lot easier get information without loops need.

It’s really simple, we can create object, properties from variables and assign it a value:

To see all this object converted in string, just put it out in a console with console.log(jsonstr):

Now starts my favorite part … access information without loops. We are using ‘user’ property as index then we can go directly to get it.

That’s all folks!