noborus / trdsql

CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.
https://noborus.github.io/trdsql/
MIT License
1.96k stars 73 forks source link

Not able to use JSON_EXTRACT #161

Closed aborruso closed 2 years ago

aborruso commented 2 years ago

Hi, first of all thank you for this great tool.

I'm trying to use the documentation page to reply JSON_EXTRACT example, My input is

[
  {
    "id": 1,
    "name": "Drolet",
    "attribute": { "country": "Maldives", "color": "burlywood" }
  },
  {
    "id": 2,
    "name": "Shelly",
    "attribute": { "country": "Yemen", "color": "plum" }
  },
  {
    "id": 3,
    "name": "Tuck",
    "attribute": { "country": "Mayotte", "color": "antiquewhite" }
  }
]

If I run the below command I have nothing.

trdsql -ijson "SELECT id, name, JSON_EXTRACT(attribute,'$country'), JSON_EXTRACT(attribute,'$color') FROM sample2.json"

I'm using trdsql version v0.9.0.

What's wrong in my command?

Thank you

aborruso commented 2 years ago

Solved. The default driver is sqlite, then I have loaded json1 extension and changed a little the syntax (in example from $country to $.country:

trdsql -ijson "SELECT id, name, JSON_EXTRACT(attribute,'$.country'), JSON_EXTRACT(attribute,'$.color') FROM tmp.json"
noborus commented 2 years ago

Thank you for the issue. JSON_EXTRACT (attribute,'$ .country') is correct as you wrote.

It's a mistake that json_extract doesn't output anything on error, but it seems to be a problem with sqlite3.