PostgreSQL - JSON/JSONB Database

Has anyone worked with JSON/JSONB databases in CW?

CREATE TABLE test(id int, data JSONB, PRIMARY KEY (id));

Anyway to use the IDE and standard ABC File classes w/ ODBC against it?

Or would you have to hand code everything?

https://scalegrid.io/blog/using-jsonb-in-postgresql-how-to-effectively-store-index-json-data-in-postgresql/

Hi Kevin,

I havenā€™t, and I hope I never have to. Think of the Clarion dictionary as a mapping between the data that Clarion works with, which comes in in the record buffer, and rows and columns in the database. Usually that is a nice, simple, mapping: The CustomerName in the Customer table maps to the Customer name field in Clarions Customer table. When you are dealing with JSON data, that mapping is no longer simple, and the only tool you really have to help you is the PROP:Name of the field (and possibly also the table. That is going to get messy awful fast.

You might get a better idea looking at the example here:

where in their simple example they have:

SELECT info ->> ā€˜customerā€™ AS customer
FROM orders
WHERE info ā†’ ā€˜itemsā€™ ->> ā€˜productā€™ = ā€˜Diaperā€™;

In Clarion you might set this up with two tables: orders and order_items, but the field ā€œproductā€ in order_items is gotten from the database by getting "info ā†’ ā€˜itemsā€™ ->> ā€˜productā€™: from the orders table in the database. So in Clarion I think you would give ā€œOrdersā€ as the Postgres table name for the order_item table in Clarion, and that mess in the double quotes above would be the external name for the column. If you could stop there it might be possible, but still very messy and probably difficult to maintain and debug, but go down a bit and you have

SELECT info ->> ā€˜customerā€™ AS customer,
info ā†’ ā€˜itemsā€™ ->> ā€˜productā€™ AS product
FROM orders
WHERE CAST ( info ā†’ ā€˜itemsā€™ ->> ā€˜qtyā€™ AS INTEGER) = 2

Now you have to wrap cast ā€¦ as integer around your column name.

So short answer: for simple stuff you might be able to use PROP:Name and get clarion to do things without having to handcode your prop:sql. But there may be some relatively simple things that you want to do that Clarion will not be able to do, and then you will be in messy handcode real fast.

1 Like

Yeah, I was not expecting a resounding ā€˜yesā€™. It would require a new File IO subsystem relying heavily on tools like jFiles moving data into and out of our standard CW structures all in the background. I was just reading about it and found it interesting - a lot like NoSQL is interesting, but i donā€™t want to use it, :slight_smile:

JSON and XML are great for transferring data between systems. If the source system decides it needs to collect another attribute it can add that attribute to the JSON file it exports, and the receiving system can just ignore that new attribute until it feels like dealing with it. Clarion programs, along with most business processes, is designed to deal with structured information. If thereā€™s a lot of stuff you donā€™t know what to do with and donā€™t care about, then a sensible first step is just to take the stuff you do care about and put it in a structure where you can deal with it. Whether you use jFiles/whatever or the database to ā€œreadā€ the JSON/XML into tables is up to you: the first method makes it easier to switch databases but is possibly more time-consuming to write and maintain.

I just went through this with reading XML; I originally set it up using Oracleā€™s tools to read the XML, then decided that a SQLite database was a more sensible target for the data. So I couldnā€™t use the Oracle code any more. The clarion code (using Robert Paresiā€™s iQ-XML) was much larger and clunkier and I had to start from scratch.

1 Like

Kevin,

Iā€™m with you on trying to ease it up. Iā€™ve found it very useful to have JSON in a database. Bringing in a JSON object so far was to read it as a CSTRING. Best way was then to use a JFiles object to manipulate the JSON and then write it back using the string again. Iā€™m sure there could be a way to use a BLOB type instead of the long CSTRING, just to make sure that I donā€™t run into a size limit. My project so far didnā€™t require much of the JSON values, so I donā€™t have answers yet.

1 Like