5

I am retrieving data from the Google Places API and casting it directly into objects for use elsewhere.

The problem is that sometimes the responses do not have all of the fields, so this causes my app to crash when the objects can't properly be created due to missing fields in the JSON response.

Here is an example of what I am talking about:

say the format of a response for example is this:

{ "a" : "some_str", "b" : "some_str", "c" : "some_str" }

but once in a while field "b" will be missing, so the response looks like this:

{ "a" : "some_str", "c" : "some_str" }

How can I account for this when I try to parse the JSON data into objects?

For example, here is a code that I would use to parse the data:

this.http.get(URL).subscribe(details => {
    let detailsObj = details.json();
    let myObj: SomeObject = {
        "fieldA" : detailsObj.a,
        "fieldB" : detailsObj.b,
        "fieldC" : detailsObj.c,
    }
});

If the field "b" doesn't exist on the JSON response, the value will be undefined on the "detailsObj" which causes a runtime error as it is trying to create an object with an undefined field.

How can I still create the object despite the partially undefined data? Ideally the data that is missing could be filled with a null.

Any pointers in the right direction would be greatly appreciated, thank you for your time!

1
  • Your app wont crash here. It will pass undefined. Commented Jan 23, 2018 at 6:19

3 Answers 3

3

Use || to fill in null when b is missing:

"fieldB" : detailsObj.b || null,

or be more robust and specifically check for undefined:

"fieldB" : detailsObj.b === undefined ? null : detailsObj.b,

The first option is more concise and easier to read, but it will set the value to null if b is 0 or false or some other falsey value.

Sign up to request clarification or add additional context in comments.

3 Comments

And why should null be better than undefined ??
@Dem Pilafian Great thank you this is what I was looking for, I knew there was a way to do it.
@JonasW. Generally, null explicitly means "does not exist" whereas undefined means "no information is provided". While the two are often technically interchangeable, the semantic meaning is subtly different. In the OP's question, it seems that undefined was causing problems whereas null works.
1

Have you tried the LoDash _.get function? You can do

Const _ = require("lodash");
"fieldA": _.get(detailsObj, "a", null);

2 Comments

My last project we used lodash extensively and _.get() was a very useful function for issues like this.
It's especially good when you have deeply nested objects and you want to avoid objectA.objectB.objectC statements.
0

you can try this :

interface SampleInterface {

    a ?: string;
    b ?: string;
    c ?: string;
}

var obj = { "a" : "some_str1", "c" : "some_str3" };

 sampleInterface : SampleInterface = obj;
 console.log(SampleInterface.b);

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.