30

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?

3
  • Your example doesn't make much sense to me. Firstly you can't declare output on the this scope like that, secondly it would be handy if you could provide the exact error you are getting Commented Aug 30, 2013 at 8:49
  • I posted solutions I made for my problem but they don't work fine. All I need to do is to correctly create a JSON object and initialize it in my TypeScript method. Code I posted is compileable - it doesn't show any errors but in console I can see TypeError "this.output is not defined" Commented Aug 30, 2013 at 9:48
  • Well, the correct solution would be to declare output on the scope of this Commented Aug 30, 2013 at 12:34

3 Answers 3

24

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;
Sign up to request clarification or add additional context in comments.

2 Comments

JSON is not the type of a JSON object, it's the type of the JSON parsing object itself.
@RyanCavanaugh 💡💡💡 This is such a valuable insight, hidden away in a comment! I was going to ask you the logical follow-up question, which is: What is the correct type for JSON data but after simply checking the return type of JSON.parse(), the answer seems to be any 😑
14

You can also do the following

const rememberUser:JSON = <JSON><unknown>{
        "username": this.credentialsForm.value.username,
        "password": this.credentialsForm.value.password
      }

Comments

10

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

var request: any = {};
request.allocation = allocationFigure;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.