Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing JSON object in TypeScript

I'm new to TypeScript and I'm stuck at working with JSON. I need to create a simple JSON object and I keep on failing doing so. Here are my first attempts:

output: JSON; //declaration
this.output = {
"col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"},
"col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, 
"col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} 
}

This doesn't work. I guess I should work with JSON.stringify function. Here's my attempt:

obj: any; //new object declaration
this.obj = {
"col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"},
"col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, 
"col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} 
}
this.output.stringify(this.obj);

But this still invokes TypeError. So to summarize my question: how to properly create and initialize JSON object in TypeScript?

like image 256
OutOfSpaceHoneyBadger Avatar asked Aug 30 '13 08:08

OutOfSpaceHoneyBadger


People also ask

How do I create a JSON object in TypeScript?

obj: any; //new object declaration this. obj = { "col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"}, "col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, "col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} } this. output.

How do you initialize an empty JSON object in TypeScript?

Use type assertions to initialize a typed variable to an empty object, e.g. const a1 = {} as Animal; . You can then set the properties on the object using dot or bracket notation. All of the properties you set on the object need to conform to the type. Copied!

How do I parse a JSON to a TypeScript object?

Use the JSON. parse() method to parse a JSON string in TypeScript, e.g. const result: Person = JSON. parse(jsonStr) . The method parses a JSON string and returns the corresponding value.


3 Answers

I've eventually figured it out. All I had to do was to create data to "any" variable like this:

output: JSON;
obj: any = 
{
"col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"},
"col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, 
"col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} 
};

and then cast it to JSON object:

this.output = <JSON>this.obj;
like image 195
OutOfSpaceHoneyBadger Avatar answered Oct 22 '22 17:10

OutOfSpaceHoneyBadger


You can also do the following

const rememberUser:JSON = <JSON><unknown>{
        "username": this.credentialsForm.value.username,
        "password": this.credentialsForm.value.password
      }
like image 27
kathikeyan A Avatar answered Oct 22 '22 17:10

kathikeyan A


It worked for me in Angular 2.4.0 Stable by doing this:

var request: any = {};
request.allocation = allocationFigure;
like image 10
Abdeali Chandanwala Avatar answered Oct 22 '22 17:10

Abdeali Chandanwala