I have an application with RESTful services. I get a JSON object from a server but I don't know the keys inside of this object. Is it restricted to define a class for JSON objects with specific keys? How can I store the JSON object that I got from the server?
-
2If you get it from the server and assign it to a variable it is already stored. Actually I don't really understand your question. Can you please add some code that demonstrates what you try to accomplish? – Günter Zöchbauer Sep 06 '16 at 08:06
-
When I define a variable as public data: json; and assign the object that I get from the server to this variable, data is being undefined. I meant isn't there a json type in Angular2? – Bünyamin Sarıgül Sep 06 '16 at 08:20
-
No, typescript has type . Angular has no type – Davut Gürbüz Sep 06 '16 at 08:22
-
Bunyamin , how are you planning to work with not known properties ? If I'm wrong correct me you meant object attributes/properties by `keys` ? Use Json.et for .net and Jackson for java to map your json to objects. – Davut Gürbüz Sep 06 '16 at 08:24
-
One question sounds like http://stackoverflow.com/questions/22885995/how-do-i-initialize-a-typescript-object-with-a-json-object. The other seems to be that you are accessing the value where it doesn't yet have a value but that's hard to tell without seeing your code. – Günter Zöchbauer Sep 06 '16 at 08:24
-
@DavutGürbüz I need specific keys and I will check them if they exists. – Bünyamin Sarıgül Sep 06 '16 at 08:26
-
If you are owner of server side as well. You can return `"class:com.project.Person"` to define Json object type and map accordingly. – Davut Gürbüz Sep 06 '16 at 08:27
-
@GünterZöchbauer oh, you are right. I made a rookie mistake and check the data with console.log(). When I gave it to the view, it comes asynchronously. Thanks. – Bünyamin Sarıgül Sep 06 '16 at 08:28
-
@DavutGürbüz I am not the owner of the server, just implementing the front-end. I don't use java or .net. I'm working with Angular2. – Bünyamin Sarıgül Sep 06 '16 at 08:32
-
So as discussed angular is not typesafe, think about if you need a typed object and implement your front-end accordingly. But if you were owener of your backend you would create a wrapper object for your response and put object metadata with reflection in it aside data. Anyways. Good luck on your problem. – Davut Gürbüz Sep 06 '16 at 08:38
-
@DavutGürbüz This is what is meant by `key` and `value`in a JSON object: `{key: value}` – RicoBrassers Sep 06 '16 at 09:05
-
@BünyaminSarıgül Do you mean something like this? http://stackoverflow.com/a/684692/6774296 Looping through the JSON object to get all `key`s, even those, which are unknown to you? – RicoBrassers Sep 06 '16 at 09:13
-
@RicoBrassers Yes, It's clear now. I couldn't comprehend at first glance because question was asking about defining a class on Json... I mapped it to Json to java/.net Object problem but it appeared as frontend only. It is still unclear to me what is trying to achieve with this. There is a service returning different type of objects and we will build its screen ??? If you can elaborate more... – Davut Gürbüz Sep 06 '16 at 09:26
-
@DavutGürbüz I think, it's the following problem: There is a (independent) service returning JSON Objects and he builds a frontend for that, but he doesn't have access to the server (code) directly. – RicoBrassers Sep 06 '16 at 09:32
-
@RicoBrassers Yes, you are right. I solved the problem and it was a simple mistake actually. Thank you all for your attention. – Bünyamin Sarıgül Sep 06 '16 at 10:11
-
@BünyaminSarıgül You might want to write a answer to your question and mark the question as solved. ;) – RicoBrassers Sep 06 '16 at 10:12
2 Answers
I get a JSON object from a server but I don't know the keys inside of this object.
Using RESTful serivce, you should know what the structure of data is. You can check what data you get with some tools like Postman(Chrome) or SOAP UI. That kind of service is a more loose idea than communication between for exampletwo Java applications using serialization (when you have two strictly typed classes). You can do something like this:
var d = new MyRichObject();
d.copyInto(jsonResult);
(How do I cast a JSON object to a typescript class)
I don't know the keys inside of this object
You can check the object for keys.
if ('key' in myObj)
so you can determinate what data you get from service.
-
The server doesn't give me the same structure every time. There may be additional keys for different queries. – Bünyamin Sarıgül Sep 06 '16 at 08:22
-
You can either standardize REST service or check for having specific keys in response. – jmachnik Sep 06 '16 at 08:25
-
@BünyaminSarıgül, if you're writing a class to describe an object with a known structure, but some fields can be absent, and make those fields optional. – Ronald Zarīts Sep 06 '16 at 08:26
-
-
@JacobM I think the problem here is, that he does not have access to the server code directly, because it's a third party service. So he can't use standardized REST structures, if they are not implemented serverside or not/poorly documented. – RicoBrassers Sep 06 '16 at 09:33
-
Yup. Realized that after his comments above. The only way now is to check object keys and parse json to corresponding class. – jmachnik Sep 06 '16 at 10:26
I was trying to assign the object out of the asynchronous function, so it was undefined since the function not responded yet. I thought its reason was that we cannot assign JSON object without creating a class with specified attributes, but I was totally wrong. I assigned the object to a variable inside of the async function and now I can access all attributes in it.

- 3,031
- 4
- 30
- 55