Flatten JSON Object to JSONpath Array: when nested object is empty, no paths are returned

Issue #73 resolved
Ivan Yakushchenko created an issue

For example, there is such JSON:

{
  "HW": {
    "AI": {}
  }
}

For this JSON, AI subobject is empty, but HW object contains AI subobject.

But, when this JSON is used as input for “Flatten JSON Object to JSONpath Array.vi” - Paths and <JSON>Items outputs are empty.

When I set “Maximum Depth” = 2, then it returns “$.HW.AI” path. But when “Maximum Depth” = -1, then it returns paths to last item if it contains value.

So maybe it would be better to do it in a way, that when “Maximum Depth” = -1, but last item is empty - let it return that last item path and its (empty) value.

Comments (9)

  1. James Powell repo owner

    Unfortunately there are many uncertain questions in making an API like this. I see the problem, but don’t much like the proposed solution. In what types of use cases is this an issue?

  2. James Powell repo owner

    This is part of a class of issues due to the fact that null, {}, [], and ““ (and NaN) are all ways of saying “nothing”, yet are also all different.

  3. Ivan Yakushchenko reporter

    Thank you for the answer.

    Actually, when node has value null, [], NaN and ““ - “JSON Object to JSONpath Array.vi” returns values. For example, for such JSON:

    {
      "HW": {
        "AI": null
      }
    }
    

    output is “$.HW.AI”.

    So it means that now VI works for all “null” or “empty” objects except {}. But also for {} it works - just “Depth value” should be set to some reasonable value.

    That’s why it a bit confusing.

    In my case it does the issue because I work on GUI for JSON editing. When I add new parameter there, which is cluster - it should be empty by default. Cluster in JSON is represented by {} syntax. So, when I add new parameter like this “Dummy parameter” :{}, and then want to display it in tree view which uses “JSON Object to JSONpath Array.vi” to get all paths in the JSON, it ignores that empty parameter. Workaround for now is to add “Dummy parameter” : {“Parameter“ : “null”}; but then user has to remove it from the tree, etc.

  4. James Powell repo owner

    In JSON terminology only {} is a “JSON Object” (things in general are called “JSON Values” not “Objects”). I think you are thinking of Arrays and scalar values as if they are also “Objects”.

  5. James Powell repo owner

    Another possible function would be one that returns all Nodes in a tree. For your initial example

    {
      "HW": {
        "AI": {}
      }
    }
    

    This would return:

    • $.HW → {“AI”:{}}
    • $.HW.AI → {}

    Would that help at all? One would have an option to only return the values if they are not themselves Objects. Then you would get a list of paths that can be used to generate a tree.

  6. Ivan Yakushchenko reporter

    Thank you, now I understand.

    In order to build tree, it would be helpful to have function, which will return all paths and values for those paths, regardless whether it is object or not. For example, when there is array in JSON now, current function returns path just to the array. But it would be helpful to get path to each array’s element. Of course it is possible to implement it using functions which are available now, but I believe that such use case could be common, so it would be great to have it in the toolkit if it is possible…

  7. Log in to comment