Ad Widget

Collapse

Preprocessing failed due to SyntaxError in (valid) JSON?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viktorkho
    Member
    • Jul 2013
    • 90

    #1

    Preprocessing failed due to SyntaxError in (valid) JSON?

    I'm going to make host LLD from JSON-over-HTTP data. The issue looks quite simple, but.. I receive a SyntaxError from my Zabbix 6.4.8

    Code:
    Preprocessing failed for: [.. {.. "Id": "48edc41c-0000-0000-ac7b-58cc77c9092d",.. "Name": "F-12",.. "Type": "Farm...
    1. Failed: SyntaxError: parse error (line 3)​
    and I have no idea whether the error relates to JS-preprocessing, the JSON, or just a bug​.. or what I am doing wrong?

    I've tested my preprocessing with https://www.jdoodle.com/ia/R5G

    The JSON document passes validation on https://jsonformatter.org/json-parser/af2e0e, I pastebined it https://pastebin.com/raw/ijH5kacA and this URL is hardcoded in this test template:

    Code:
    zabbix_export:
      version: '6.4'
      template_groups:
        - uuid: ea8da0a2b67548b2984e3db9e32283e3
          name: Templates/Test
      host_groups:
        - uuid: c0b64a45e6b344b38576b90e0823dfba
          name: Test
      templates:
        - uuid: 7f897c9d7a894e709a1de601260ae373
          template: 'Test Preprocessing failed SyntaxError'
          name: 'Test Preprocessing failed SyntaxError'
          groups:
            - name: Templates/Test
          discovery_rules:
            - uuid: f759f7fa15c14a72895b293bcc3c8c8d
              name: Houses
              type: HTTP_AGENT
              key: houses
              host_prototypes:
                - uuid: a5bdda6c16b8408ebe116cb34b824302
                  host: '{#LOCATION_ID}'
                  name: '{#LOCATION_NAME}'
                  group_links:
                    - group:
                        name: Test
              url: 'https://pastebin.com/raw/ijH5kacA'
              lld_macro_paths:
                - lld_macro: '{#LOCATION_ID}'
                  path: $.Id
                - lld_macro: '{#LOCATION_NAME}'
                  path: $.Name
              preprocessing:
                - type: JAVASCRIPT
                  parameters:
                    - |
                      // Get Houses Id and Name
                      var params = JSON.parse(value)
                      return JSON.stringify(params[0].Children.map((house) => ({"Id": house.Id, "Name": house.Name})))
                - type: STR_REPLACE
                  parameters:
                    - /
                    - .
    Can you please check whats wrong?​
  • viktorkho
    Member
    • Jul 2013
    • 90

    #2
    Seems that Duktape (https://duktape.org/api.html) doesn't support `map()` syntax, so "SyntaxError: parse error" is about JS-code parsing, not ingesting JSON

    Comment

    • markfree
      Senior Member
      • Apr 2019
      • 868

      #3
      I think it was a bit unclear of what preprocessing you are trying to use and what kind of data you are trying to extract from your JSON.
      I would also point out that a template Yaml is not the best option to present your preprocessing steps. A simple print of your item would suffice.

      Anyway, as your template shows, your JavaScript preprocessing step is the following.

      Code:
      // Get Houses Id and Name
      var params = JSON.parse(value)
      return JSON.stringify(params[0].Children.map((house) => ({"Id": house.Id, "Name": house.Name})))
      And when you test it, the error is this.
      Failed: SyntaxError: parse error (line 3)
      Right?

      So, why are you parsing the JSON with JavaScript when you can use JSONPath?​

      Comment

      • cyber
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Dec 2006
        • 4807

        #4
        Originally posted by markfree
        I think it was a bit unclear of what preprocessing you are trying to use and what kind of data you are trying to extract from your JSON.
        ...
        So, why are you parsing the JSON with JavaScript when you can use JSONPath?​
        I think he is trying to format check output as json (JSON.stringify... ) and after that point LLD macros to certain paths in json (path: $.Id, path: $.Name)​

        Comment

        • markfree
          Senior Member
          • Apr 2019
          • 868

          #5
          I thought the original value was a JSON already.

          Comment

          Working...