-1

I need to read a json file and print the data on a table The json file has a lot of objects, and objects inside objects, arrays...

Example of the json file

[
  {
    "id_transaction": 116,
    "company": "ABC",
    "verified": true,
    "date": "2019-04-09T13:31:20.429Z",
    "bio_data": {
      "id": "string",
      "data": "string",
      "digital": [
        {
          "name": "value1",
          "digital": ""
        },
        {
          "name": "value2",
          "digital": "value2"
        },
        {
          "name": "",
          "digital": "value3"
        }       
      ]
    },
    "info_data": {
      "value1": "value1",
      "value2": "",
      "value3": "",
      "value4": "value4",
      "value5": 1.0,
      "value6": ""      
    }
  }
]

Any data of this json file can be null, the objects bio_data and info_data can be null, the array digital can be null, and the others simple atributs (company, date etc..) can be null.

What's the best way to deal with null values? For now, I was dealing with each case, changing the null value to -, but that's not the most intelligent way. Follow an example:

id_transaction !== null ? id_transaction : ' - '
info_data !== null ? info_data.value1 : ' - '
bio_data.digital[0] !== undefined ? bio_data.digital[0].name] : '-'

How can I create a fucntion to deal with it? Or exists a better way to deal with it (i'm using react)?

S_A
  • 411
  • 1
  • 7
  • 25
  • 3
    This is not valid JSON - `"value4": boolean` would not parse. – VLAZ Jul 04 '19 at 13:17
  • Why do you need to deal with it? If you're just printing it to a table why does it matter if it is an empty value? Just display it as an empty value then? – ComputerLocus Jul 04 '19 at 13:18
  • Sorry, I edited the question. I need to change the values because those values are sensitive, that's why json example was wrong – S_A Jul 04 '19 at 13:18
  • @S_A what' wrong with be null? There is totally right to use null if it's null.. –  Jul 04 '19 at 13:21
  • 1
    A simpler way of writing `id_transaction !== null ? id_transaction : ' - '` is `id_transaction || ' - '` if you don't mind other falsy values like empty strings and 0 also being potentially changed – Bali Balo Jul 04 '19 at 13:22
  • the problem is this, supose that I want to access this attribute: `bio_data.digital[0].name`, if digital is null, I got an error – S_A Jul 04 '19 at 13:23
  • 3
    If you are using `JSON.parse` you can also use a second "[`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter)" parameter letting you transform your data. You can change all the nulls to `' - '` using `JSON.parse('{ "a": null }', (key, value) => value === null ? ' - ' : value)` – Bali Balo Jul 04 '19 at 13:26
  • Not getting any exception.. https://jsfiddle.net/d34m758r/ Show us the code that is doing the exception –  Jul 04 '19 at 13:32
  • @William if `"digital": []`, I got the error `TypeError: data.bio_data.digital[0] is undefined` – S_A Jul 04 '19 at 13:34
  • 1
    @S_A If print normally you wont get any error, if you try to assign to something, there you will get an error. Just try catch around. [https://jsfiddle.net/d34m758r/](https://jsfiddle.net/d34m758r/2/) –  Jul 04 '19 at 13:40

1 Answers1

1

Well, what should your application do when any of the properties are null? Should it:

  • skip that property?
  • use a default value such as '-'?
  • throw an exception?

You'll need to specify that yourself, there is no standard answer.

Once you have specified that, you can use the and- and or-operators (&& and ||) to shorten your code a bit. For example:

let name = bio_data.digital && bio_data.digital[0] && bio_data.digital[0].name || 'Unknown';

See also:

Grilse
  • 3,491
  • 2
  • 28
  • 35