question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Got a null object in array

See original GitHub issue

Hi everyone,

I would like to select themes that have an itemsarray.

Data origin :

{
  "themes": [
    {
      "id": "122jj23164-8202-4da2-901e-1237f1236",
      "name": "Push Products N1"
    },
    {
      "id": "123fklj786-8202-4da2-901e-87123873",
      "name": "Push Products N2",
      "items": {
        "item": [
          {
            "id": 19812,
             "attribute": [
                {
                  "name": "second-id",
                  "value": {
                    "content": 19812
                  }
                },
                {
                  "name": "wording",
                  "value": {
                    "content": "A product"
                  }
                }
              ]
          },
          {
            "id": 87613,
            "attribute": [
              {
                "name": "second-id",
                "value": {
                  "content": 87613
                }
              },
              {
                "name": "wording",
                "value": {
                  "content": "Sample"
                }
              }
            ]
          }
        ]
      }
    }
  ]
}

Jolt :

[
  {
    "operation": "shift",
    "spec": {
      "themes": {
        "*": {
          "items": {
            "@(1,id)": "themes[&2].id",
            "@(1,name)": "themes[&2].name",
            "item": {
              "*": {
                "id": "themes[&4].products[].id",
                "attribute": {
                  "*": {
                    "value": {
                      "content": "themes[&7].products[&4].@(2,name)"
                    }
                  }
                }
              },
              "id": "themes[&3].products[&3].id",
              "attribute": {
                "*": {
                  "value": {
                    "content": "themes[&6].products[&6].@(2,name)"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

I don’t understand why I got a null at the first index on my themes array.

{
  "themes" : [ null, {
    "id" : "123fklj786-8202-4da2-901e-87123873",
    "name" : "Push Products N2",
    "products" : [ {
      "id" : 19812,
      "second-id" : 19812,
      "wording" : "A product"
    }, {
      "id" : 87613,
      "second-id" : 87613,
      "wording" : "Sample"
    } ]
  } ]
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
milosimpsoncommented, May 4, 2017

You need to do two shifts.

First shift only passes thru elements of the “theme” array that are “valid”, where valid means I think means “it has at least one element of in the item.items array”.

Then do the rest of the transform as normal.

[
  {
    "operation": "shift",
    "spec": {
      "themes": {
        "*": { // visit each item in themes array
          "items": {
            "item": {
              "0": {
                // if we matched to here it menas this element
                // of the themes array has "items.item[]" with 
                // at leat one element.
                // Thus this "theme" is valid, and we will copy
                // it across.
                "@3": "temp[]"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "temp": {
        "*": {
          "id": "themes[&1].id",
          "name": "themes[&1].name",
          "items": {
            "item": {
              "*": {
                "id": "themes[&4].products[&1].id",
                "attribute": {
                  "*": {
                    "value": {
                      "content": "themes[&7].products[&4].@(2,name)"
                    }
                  }
                }
              },
              "id": "themes[&3].products[&2].id",
              "attribute": {
                "*": {
                  "value": {
                    "content": "themes[&6].products[&6].@(2,name)"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]
0reactions
Matsa59commented, May 4, 2017

Yes exactly. I was thinking JOLT do it natively by removing null object in array

Read more comments on GitHub >

github_iconTop Results From Across the Web

Null Values in array of objects - javascript - Stack Overflow
Null Values in array of objects ... I am creating objects when textbox having some values (using ng-blur and textbox.value!==undefined ) and then ......
Read more >
Is there any way to check if there is a null value in an object or ...
To check if there is a null value in an object or array, use the concept of includes(). Example. Following is the code...
Read more >
Java - Check if Array is Empty - Tutorial Kart
To check if an array is null, use equal to operator and check if array is equal to the value null. In the...
Read more >
Everything you wanted to know about $null - PowerShell
What is NULL? You can think of NULL as an unknown or empty value. A variable is NULL until you assign a value...
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
1. NullPointerException when calling an instance method · 2. Java NullPointerException while accessing/modifying field of a null object · 3. Java ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found