kellyjonbrazil / jello

CLI tool to filter JSON and JSON Lines data with Python syntax. (Similar to jq)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help with basic usage and syntax errors

DrTron opened this issue · comments

All,

I'm trying to something rather basic: extract values from lm_sensors, which I can export as json-formatted data.
However, I keep running into syntax errors I don't understand.

This is a small extract of the json-data:

{
  "aquaero-hid-3-1cb1": {
    "Adapter": "HID adapter",
    "Fan 1 voltage": {
      "in0_input": 4.24
    }
  }
}

jello -s gives me:

_ = {};
_.aquaero-hid-3-1cb4 = {};
_.aquaero-hid-3-1cb4.Adapter = "HID adapter";
_.aquaero-hid-3-1cb4["Fan 1 voltage"] = {};
_.aquaero-hid-3-1cb4["Fan 1 voltage"].in0_input = 4.24;

As expected. So to get the value for the Fan1 voltage, I try:
cat x.json | jello _.aquaero-hid-3-1cb4["Fan 1 voltage"].in0_input and
cat x.json | jello '_.aquaero-hid-3-1cb4["Fan 1 voltage"].in0_input'
That, however, gives:

jello:  Query Exception:  SyntaxError
        invalid syntax (<unknown>, line 1)
        SyntaxError:  _.aquaero-hid-3-1cb4[Fan 1 voltage].in0_input
        query:  _.aquaero-hid-3-1cb4[Fan 1 voltage].in0_input
        data:  {'aquaero-hid-3-1cb4': {'Adapter': 'HID adapter', 'Fan 1 voltage': {'in0_input': 4.24}}}

And now I'm confused. Isn't the result of "jello -s" the concatenation of levels I need to get my result?
The same thing happens for names without spaces in them, btw.
Even trying to get the first level with
cat x.json | jello _.aquaero-hid-3-1cb4
results in a Syntax error.

Thanks!

Edit: After some trial and error, it seem that the "-3-1" is something the python syntax doesn't like. I can escape the spaces, but have not found a way to get around the "-3-1"....

Hi there - thanks for reporting this! I think you are running into shell escaping issues. Could you try your jello query with single quotes around it? That way the shell won't remove the double quotes in the bracket notation:

cat x.json | jello '_.aquaero-hid-3-1cb4["Fan 1 voltage"].in0_input'

hmm... that is strange, for some reason jello doesn't like the first key in dot notation:

X ~ % cat test.json | jello '_.aquaero-hid-3-1cb4'
jello:  Query Exception:  SyntaxError
        invalid decimal literal (<unknown>, line 1)
        SyntaxError:  _.aquaero-hid-3-1cb4
        query:  _.aquaero-hid-3-1cb4
        data:  {'aquaero-hid-3-1cb1': {'Adapter': 'HID adapter', 'Fan 1 voltage': {'in0_input':
            4.24}}}

X ~ % cat test.json | jello '_'                   
{
  "aquaero-hid-3-1cb1": {
    "Adapter": "HID adapter",
    "Fan 1 voltage": {
      "in0_input": 4.24
    }
  }
}
√ ~ % cat test.json | jello '_["aquaero-hid-3-1cb1"]'
{
  "Adapter": "HID adapter",
  "Fan 1 voltage": {
    "in0_input": 4.24
  }
}

It works in bracket notation, but I'm not sure why. I'll have to investigate.

Something about the name of the key makes the python interpreter think that could be a valid number?

When I change the dashes to underscores it works. I've never really tried using dashes in dictionary keys before so I haven't run into this. Since keys are typically strings it probably doesn't matter, but when using dot notation it's no longer a string but an object name in python so a dash in the name might not be legal.

√ ~ % cat test2.json| jello
{
  "aquaero_hid_3_1cb1": {
    "Adapter": "HID adapter",
    "Fan 1 voltage": {
      "in0_input": 4.24
    }
  }
}
√ ~ % cat test2.json| jello _.aquaero_hid_3_1cb1
{
  "Adapter": "HID adapter",
  "Fan 1 voltage": {
    "in0_input": 4.24
  }
}

Looks like, dashes and other characters cannot be in object names. I'll fix the schema output so any non-valid object names are encapsulated in brackets.

>>> hello-world = 1
  File "<stdin>", line 1
    hello-world = 1
    ^^^^^^^^^^^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?

variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character (_). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.

Unicode characters can also be used in object names since Python3.

Here is how I can fix the schema generation code:
https://stackoverflow.com/questions/36330860/pythonically-check-if-a-variable-name-is-valid

Thanks!
I was starting to think I was just too stupid to get simple commands working.
To be fair, "jq" also runs into an error. Probably also because issues with shell escaping.
But yes, the dashes seem to be what's causing the issue. Sadly, as the output of lm_sensors contains them, I have no way of getting rid of them.

No worries - can't believe I haven't run into this before. I will fix this and add tests. In the mean time, the best way forward would be to use bracket syntax for keys like that.

I'll try to find the correct syntax. The spaces further down don't make it easier, though ;-)

You can use bracket notation for all keys like this:

cat x.json | jello '_["aquaero-hid-3-1cb1"]["Fan 1 voltage"]["in0_input"]'

Basically just realize your are working with a dictionary in python (_ is the name of the dictionary), so you can do anything you would normally do with a dictionary or a list of dictionaries.

I have a fix for this in the dev branch. The Schema output now checks for invalid or reserved keynames and converts them to bracket syntax.

fixed in v1.5.5.