Solved

Why is my JSON parser outputting a string as a true boolean?

  • 10 April 2023
  • 9 replies
  • 268 views

Userlevel 2
Badge +4

I have an automator that is to detect devices with low disk space on C:\ by leveraging the component data. API endpoint is my mydomain.freshservice.com/api/v2/assets/{{asset.display_id}}/components.

Data from GET request:

{
  "components": [
    {
      "id": 13000768962,
      "created_at": "2020-02-02T08:46:06Z",
      "updated_at": "2023-04-10T02:18:31Z",
      "component_type": "Logical Drive",
      "component_data": [
        {
          "drive_name": "C",
          "file_type": "NTFS",
          "capacity": "952",
          "drive_type": "Local",
          "free_space": "764"
        }
      ]
    },
    {
      "id": 13000768963,
      "created_at": "2020-02-02T08:46:06Z",
      "updated_at": "2020-02-02T08:46:06Z",
      "component_type": "Processor",
      "component_data": [
        {
          "model": "Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz",
          "manufacturer": "GenuineIntel",
          "no_of_cores": "4",
          "cpu_speed": "1.992"
        }
      ]
    },
    {
      "id": 13000768964,
      "created_at": "2020-02-02T08:46:06Z",
      "updated_at": "2023-04-05T11:46:19Z",
      "component_type": "Network Adapter",
      "component_data": [
        {
          "nic": " Intel(R) Wireless-AC 9560 160MHz",
          "mac_address": "AA:BB:CC:DD:EE:FF",
          "ip_addr": "192.168.0.33",
          "dhcp_enabled": "True"
        }
      ]
    },
    {
      "id": 13000768965,
      "created_at": "2020-02-02T08:46:06Z",
      "updated_at": "2020-02-02T08:46:06Z",
      "component_type": "Memory",
      "component_data": [
        {
          "speed": "2133 Mhz",
          "capacity": "8",
          "socket": "ChannelA-DIMM0",
          "memory_type": "RDRAM"
        },
        {
          "speed": "2133 Mhz",
          "capacity": "8",
          "socket": "ChannelB-DIMM0",
          "memory_type": "RDRAM"
        }
      ]
    }
  ]
}

 

I then use these JSON paths to get two corresponding String values:

$.components.[?(@.drive_name=='C')].free_space
Returns: [“764”]

$.components.[?(@.drive_name=='C')].capacity
Returns: [“952”]

 

I then go to remove the [“ characters with expression builders, so that I can use a mathematical condition, but run into an issue - the {{P1.free_space}} and {{P2.capacity}} placeholders evaluate as “true” for both, as if they were meant to be a boolean, and I can’t figure out why.

 

Note: I also realise if I could format the JSONpath in a way that I can get just the values 764 and 952 without being in a JSON format, that I could completely bypass this issue.

Thanks in advance for any assistance!

Regards,
Cody

icon

Best answer by cody.pieper 12 April 2023, 00:57

View original

9 replies

Userlevel 7
Badge +16

Hello @cody.pieper, one thing I would do is use the workflow automator and add a note to a ticket that output the values you are getting for capacity and free_space. Maybe you have already done whats necessary to validate the values you are getting from these fields, and if so could you share a screenshot of those values? This could help us troubleshoot further. If the note outputs the two numbers as strings accordingly then we at least know that we are starting with correct data before using an expression to alter those values.

TIA

Userlevel 2
Badge +4

Hello @cody.pieper, one thing I would do is use the workflow automator and add a note to a ticket that output the values you are getting for capacity and free_space. Maybe you have already done whats necessary to validate the values you are getting from these fields, and if so could you share a screenshot of those values? This could help us troubleshoot further. If the note outputs the two numbers as strings accordingly then we at least know that we are starting with correct data before using an expression to alter those values.

TIA

Thanks Zachary!

Here’s the JSON parser node with the two values:

 

 

I’ve been sending the test data to myself as emails:

 

 

This is what I receive:
 

 

 

So it does appear that the JSON parser outputs the wrong values, hopefully I’m not mistaken!

Userlevel 7
Badge +13

Hello @cody.pieper, one thing I would do is use the workflow automator and add a note to a ticket that output the values you are getting for capacity and free_space. Maybe you have already done whats necessary to validate the values you are getting from these fields, and if so could you share a screenshot of those values? This could help us troubleshoot further. If the note outputs the two numbers as strings accordingly then we at least know that we are starting with correct data before using an expression to alter those values.

TIA

Thanks Zachary!

Here’s the JSON parser node with the two values:

 

 

I’ve been sending the test data to myself as emails:

 

 

This is what I receive:
 

 

 

So it does appear that the JSON parser outputs the wrong values, hopefully I’m not mistaken!

Hello, 

Have you tried to change the type in the JSON parser?

 

//Daniel 

Userlevel 7
Badge +16

Good suggestion @Daniel Söderlund. @cody.pieper, if you remove the quotations from around the two fields in the “Structured View” then generate the output again, it will change the type.

Here id is a string
Here id is a number

Hopefully that allows you to get the correct value.

Userlevel 2
Badge +4

Hey Daniel and Zachary,

Thanks for the great idea - I’ve attempted that but unfortunately, still all outputting as ‘true’ regardless of being any of strings, numbers or arrays of them.

I have a Zoom call with a Freshservice product specialist in 15 minutes - if they can find anything, I’ll report back what is going on.

Thanks again for the help so far!
Cody

 

 

 

 

 

 

Userlevel 2
Badge +4

The product specialist has hit gold for me, thankfully!

So instead of generating the JSON parser values with the generate button, I made the two values manually. This isn’t the issue specially, but it caused it. The issue is that in the actual automation, the parser needs the heirarchy of variables all the way up to the root object to find the values.

Oddly, the JSON parser preview section displays the values I want correctly, without the heirarchy being intact. Perhaps the backend for the preview section is newer/more resilient than the JSON parser code for automation - but that’s above my head.

This is what I’m now using in the JSON

 

My next challenge is to find how to get a value to output just as a number, i.e. 952 without the [“”]. In the past I have definitely had a JSON parser resolve directly into a number, but I’ve somehow lost the ability to do so with the same method (setting the type as Number and selecting the first element in the array of data).

I’ve also tried to set it as an Array of Numbers (containing only “952” and then select the first element.

Either through the JSON parser itself - $.components[*].component_data[?(@.drive_name=='C')].capacity[0]
Result: [“9”]

Or with the liquid placeholder and the | first filter - {{P2.root.components.components_object.component_data.component_data_object.capacity | first}}
Results: [

 

Anyway, I’ll mark this one as resolved, and search for existing topics for this new challenge, as it’s likely been answered, and also likely a PEBKAC issue 😁

Thanks for all of the help!

Cody

Userlevel 2
Badge +4

Good suggestion @Daniel Söderlund@cody.pieper, if you remove the quotations from around the two fields in the “Structured View” then generate the output again, it will change the type.

Here id is a string
Here id is a number

Hopefully that allows you to get the correct value.

 

That’s really interesting, and similar to what I’ve experienced in the past. I did so and noticed that it indeed changed the previews to the format [number]

To test, I made an expression to divide one number by the other to get a decimal output:

 

But the expression still fails due to invalid operands as the values still come through in the format of [‘number’]

 

 

Will keep experimenting!

Userlevel 2
Badge +4

Ahh I’m a dork!

I just re-read what Zachary expanded on from what Daniel said and realised the secondary issue I just mentioned. Although unrelated to the topic issue - that the JSON parser was setting values as ‘True’, once that was resolved I still couldn’t get the values as actual numbers.

My isuse was I didn’t realise the the JSON output of the web request api/v2/assets/{{asset.display_id}}/components shows all component values as strings - hence why the parser set them as such. Usually computer data is quite data-type accurate in Freshservice, but the components module of assets is a little more nuanced, so I can understand why String is the safe bet for all data.

Example of component data (All Strings):

                    "drive_name": "C",

                    "file_type": "NTFS",

                    "capacity": "952",

                    "drive_type": "Local",

                    "free_space": "757"

Example of asset data (Strings and Numbers)
            "os_service_pack_13000168144": "0.0",

            "memory_13000168144": 15.78,

            "disk_storage_13000168144": null,

            "disk_space_13000168144": 952,

            "cpu_speed_13000168144": 1.99,

            "cpu_core_count_13000168144": 4

But that’s fine, it just means I’ll use Expression Builder to substring out the number before using it in any maths.

Thanks all!

Userlevel 7
Badge +16

Hey @cody.pieper glad to hear that you are able to get access to the correct values from the JSON Parser. Hopefully you are able to work with the expression node to get the correct type you need for the values in the array.

Appreciate you documenting your journey through this problem here. I am sure it will prove valuable to the community!

Take care :)

Reply