Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JQ.node transformation not applied? #16

Open
alphatrianguli opened this issue Jul 7, 2024 · 3 comments
Open

JQ.node transformation not applied? #16

alphatrianguli opened this issue Jul 7, 2024 · 3 comments

Comments

@alphatrianguli
Copy link

alphatrianguli commented Jul 7, 2024

Hi Daniel

I have the impression, my jq.node transformation is not working and I cant figure out why:

This is my JSON, that I want to transform:

[
  {
    "id": 3783740201,
    "pet_id": 299635,
    "tag_id": 427802,
    "device_id": 725206,
    "where": 2,
    "since": "2024-07-06T07:03:34Z"
  },
  {
    "id": 3783430740,
    "pet_id": 299636,
    "tag_id": 427757,
    "device_id": 725206,
    "where": 2,
    "since": "2024-07-06T05:20:00Z"
  }
]

And this is my config for the module:

{
  module: 'MMM-json',
  position: 'top_right',
  header: "Cat-Location",
  config: { // 
     url: http://192.168.0.12:8080/pets/position, // Path to your json api
     refreshInterval: 60000,
     jq: '. | map(if .where == 2 then .where = "outside" elif . == 1 then .where = "inside" else . end)',
     values: [
       {
          title: "Leia",
          query: ["$[0].where", "$[0].since"],
          prefix: ["", "(since"],
          suffix:  ["",")"]
       },
       {
          title: "Luke",
          query: ["$[1].where", "$[1].since"],
          prefix: ["", "(since"],
          suffix:  ["",")"]
       }               
     ],
     styleRules: [ // Provide custom style rules for any value
       {
          match: (value) => value == "2",
          style: "color: orange; font-weight: bold;"
       },
       {
          match: (value) => value == "1",
          style: "color: lawngreen; font-weight: bold;"
       }                    
     ],
     headerIcon: "fa-cat"                 
  }
},

When I test the jq code on https://jqplay.org/ for example, it correctly replaces "2" with the string "outside" and "1" with inside. But still, on my mirror, the values 2 and 1 are displayed - it seems the jq transformation is not applied? Can you help me out here?

Many thanks! :)

@DanielHabenicht
Copy link
Owner

Hey,
this probably has to do with the package used to process jq commands in this module.
It does not use the standard jq syntax but jq-node (which is more powerful and sometimes different in syntax).
It seems like your jq syntax does not work with this package:
image

@alphatrianguli
Copy link
Author

Thanks for your answer on this! Unfortunately, I did not find out anything anywhere about that jq-node syntax... Also, are you sure, when using Multi Values as in my example above, the jq is applied at all? I've been trying very simple transformations and the JSON string never got transformed...

Nevertheless, thanks anyway. I might spin up a docker which processes the JSON with regular JQ and then passes it on to your module.

@DanielHabenicht
Copy link
Owner

Yeah it definitely works, but I've als run into issues with jq.node because it lacks documentation.
However I did choose it purposly because it works cross platform and there is no need to install jq as an extra package. Most of the other jq packages just wrap jq on commandline.

I will try to add more documentation to the jq.node package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants