I try to migrate data an old database into our new application.
In process, I need to grab data from old db to create a JSON which must be stored in a field in the new MySQL db.
So I use the components tWriteJSONField and tExtractJSONFields.
In tWriteJSONField, my XML tree looks like this :
path
|-- id [loop element]
|-- name
|-- description
N.B. : I can't find how to use loop element and group element properties. I don't understand how it works and the documentation doesn't talk about this.
The component tWriteJSONField is linked to a tExtractJSONFields in order to extract id from JSON. I need this to know to each record JSON must be linked.
tExtractJSONFields configuration : XPath request
"/path"
tExtractJSONFields configuration : Mapping
-----------------------------------------------
| column | XPath request | get nodes ? |
-----------------------------------------------
| idForm | "id" | false |
-----------------------------------------------
| jsonStructure | "*" | yes |
-----------------------------------------------
My problem is in jsonStructure output by tExtractJSONField, I only get the first child of my root tag. In my case jsonStructure looks like this :
{
"id": "123"
}
Expected result is :
{
"id": "123",
"name": "Test",
"description": "Test"
}
If I declare the child name before id for example, I will get :
{
"name": "Test"
}
I have tried to change the XPath query for jsonStructure but I never get all the fields.
Why ?
It's my first question about Talend, so if it lacks information, let me know in the comments.
Thanks for help.
EDIT :
Data from tMysqlInput to tWriteJSONField :
N.B. : My flux contains more columns but I only show you which are used to create JSON.
---------------------------------------------------------------------------------------
| IdForm | NomForm | DescrForm |
---------------------------------------------------------------------------------------
| 1 | English training | <p>This is a description of the training</p> |
---------------------------------------------------------------------------------------
| 2 | French training | <p>This contains HTML tags from a WYSIWYG</p> |
---------------------------------------------------------------------------------------
| 3 | How to use the application | <p>Description</p> |
---------------------------------------------------------------------------------------
In tWriteJSONField, columns are mapped to the JSON like this :
path
|-- id [loop element] --> IdForm
|-- name --> NomForm
|-- description --> DescrForm
tWriteJSONField output a new flux with the same columns as the input (although, this columns are all empty in the output even if they were populated in input) and add a new one jsonStructure which contains generated JSON.
This new flux is caught by a tExtractJSONFields (configuration for this component is available in my original post).
tExtractJSONFields outputs this flux :
--------------------------
| IdForm | jsonStructure |
--------------------------
| 1 | { "id": "1" } |
--------------------------
| 2 | { "id": "2" } |
--------------------------
| 3 | { "id": "3" } |
--------------------------
And I expect it returns this one :
--------------------------------------------------------------------------------------------
| IdForm | jsonStructure |
--------------------------------------------------------------------------------------------
| 1 | { "id": "1", "name": "English training", "description": "<p>This is[...]</p>" } |
--------------------------------------------------------------------------------------------
| 2 | { "id": "2", "name": "French training", "description": "<p>[...]</p>" } |
--------------------------------------------------------------------------------------------
| 3 | { "id": "3", "name": "How to use the [...]", "description": "<p>[...]</p>" } |
--------------------------------------------------------------------------------------------
EDIT 2
I use TOS 5.4.0.r110020 if it can help.
Your XPath request for JSONStructure column is not correct. Just remove "*" and you will get the expected result.
Also, if you don't need the root node in the json entry, just check "Remove root node" on tWriteJsonField and change Loop XPath Query to "/" in tExtractJSONFields
Related
My DB (MySQL) looks as follows:
TASKS:
-----------------
| id | desc |
-----------------
| 1 | 'dishes' |
| 2 | 'dust' |
-----------------
IMAGES:
---------------------------
| id | task_id | url |
---------------------------
| 1 | 1 | 'http1' |
| 2 | 1 | 'http2' |
---------------------------
I would like to get a response in the following structure (nested array of objects with id, url):
"tasks": [
{
"id": 1,
"desc": "dishes",
"images": [
{
"id": 1,
"url": "http1"
},
{
"id": 2,
"url": "http2"
}
]
},
...
]
The closest I have got was with this code:
SELECT
t.id,
t.desc,
JSON_ARRAYAGG(i.url) AS images,
FROM tasks AS t
LEFT JOIN images AS i ON t.id=i.task_id
GROUP BY t.id
And got in return:
[
{
"id": 1,
"desc": "dishes",
"images": [
"http1",
"http2"
]
}
...
]
Above response is problematic as I need the image_ids.
I have also tried using JSON_OBJECTAGG (which is not ideal) however I had below SQL error:
"JSON documents may not contain NULL member names."
Indeed some tasks may not have images matching and I want to have them included in the response.
How should I refactor my code to get the desired response from the server?
I've been experimenting with PostgreSQL's Ltree extension. I'd like to use it to store hierarchical to-do list data (i.e. lists with sub lists). It works well, but after spending a fair bit of time, I still can't find a nice way to retrieve the information from the database in hierarchical JSON format. Here is an example of what I'd like to achieve:
id | content | position | parent_id | parent_path
----+------------------------+-----------+----------+-------------+
1 | Fix lecture notes. | 1 | | root
2 | Sort out red folder. | 1 | 1 | root.1
3 | Order files. | 1 | 2 | root.1.2
4 | Label topics. | 2 | 2 | root.1.2
5 | Sort out blue folder. | 2 | 1 | root.1
6 | Look for jobs. | 2 | | root
From this, to the json output below:
[
{
"id":1,
"content":"Fix lecture notes.",
"position":1,
"parent_id":null,
"parent_path":"root",
"children":[
{
"id":2,
"content":"Sort out red folder.",
"position":1,
"parent_id":1,
"parent_path":"root.1",
"children":[
{
"id":3,
"content":"Order files.",
"position":1,
"parent_id":2,
"parent_path":"root.1.2",
"children":[]
},
{
"id":4,
"content":"Label topics.",
"position":2,
"parent_id":2,
"parent_path":"root.1.2",
"children":[]
}
]
},
{
"id":2,
"content":"Sort out blue folder.",
"position":2,
"parent_id":1,
"parent_path":"root.1",
"children":[]
}
]
},
{
"id":1,
"content":"Look for jobs.",
"position":1,
"parent_id":null,
"parent_path":"root",
"children":[]
}
]
Is there any neat way this can be done, maybe with Python server side? Looking for ideas really!
I have a MySQL table authors with columns id, name and published_books. In this, published_books is a JSON column. With sample data,
id | name | published_books
-----------------------------------------------------------------------
1 | Tina | {
| | "17e9bf8f": {
| | "name": "Book 1",
| | "tags": [
| | "self Help",
| | "Social"
| | ],
| | "language": "English",
| | "release_date": "2017-05-01"
| | },
| | "8e8b2470": {
| | "name": "Book 2",
| | "tags": [
| | "Inspirational"
| | ],
| | "language": "English",
| | "release_date": "2017-05-01"
| | }
| | }
-----------------------------------------------------------------------
2 | John | {
| | "8e8b2470": {
| | "name": "Book 4",
| | "tags": [
| | "Social"
| | ],
| | "language": "Tamil",
| | "release_date": "2017-05-01"
| | }
| | }
-----------------------------------------------------------------------
3 | Keith | {
| | "17e9bf8f": {
| | "name": "Book 5",
| | "tags": [
| | "Comedy"
| | ],
| | "language": "French",
| | "release_date": "2017-05-01"
| | },
| | "8e8b2470": {
| | "name": "Book 6",
| | "tags": [
| | "Social",
| | "Life"
| | ],
| | "language": "English",
| | "release_date": "2017-05-01"
| | }
| | }
-----------------------------------------------------------------------
As you see, the published_books column has nested JSON data (one level). JSON will have dynamic UUIDs as the keys and its values will be book details as a JSON.
I want to search for books with certain conditions and extract those books JSON data alone to return as the result.
The query that I've written,
select JSON_EXTRACT(published_books, '$.*') from authors
where JSON_CONTAINS(published_books->'$.*.language', '"English"')
and JSON_CONTAINS(published_books->'$.*.tags', '["Social"]');
This query performs the search and returns the entire published_books JSON. But I wanted just those books JSON alone.
The expected result,
result
--------
"17e9bf8f": {
"name": "Book 1",
"tags": [
"self Help",
"Social"
],
"language": "English",
"release_date": "2017-05-01"
}
-----------
"8e8b2470": {
"name": "Book 6",
"tags": [
"Social",
"Life"
],
"language": "English",
"release_date": "2017-05-01"
}
There is no JSON function yet that filters elements of a document or array with "WHERE"-like logic.
But this is a task that some people using JSON data may want to do, so the solution MySQL has provided is to use the JSON_TABLE() function to transform the JSON document into a format as if you had stored your data in a normal table. Then you can use a standard SQL WHERE clause to the fields returned.
You can't use this function in MySQL 5.7, but if you upgrade to MySQL 8.0 you can do this.
select authors.id, authors.name, books.* from authors,
json_table(published_books, '$.*'
columns(
bookid for ordinality,
name text path '$.name',
tags json path '$.tags',
language text path '$.language',
release_date date path '$.release_date')
) as books
where books.language = 'English'
and json_search(tags, 'one', 'Social') is not null;
+----+-------+--------+--------+-------------------------+----------+--------------+
| id | name | bookid | name | tags | language | release_date |
+----+-------+--------+--------+-------------------------+----------+--------------+
| 1 | Tina | 1 | Book 1 | ["self Help", "Social"] | English | 2017-05-01 |
| 3 | Keith | 2 | Book 6 | ["Social", "Life"] | English | 2017-05-01 |
+----+-------+--------+--------+-------------------------+----------+--------------+
Note that nested JSON arrays are still difficult to work with, even with JSON_TABLE(). In this example, I exposed the tags as a JSON array, and then use JSON_SEARCH() to find the tag you wanted.
I agree with Rick James — you might as well store the data in normalized tables and columns. You think that using JSON will save you some work, but it's won't. It might make it more convenient to store the data as a single JSON document instead of multiple rows across several tables, but you just have to unravel the JSON again before you can query it the way you want.
Furthermore, if you store data in JSON, you will have to solve this sort of JSON_TABLE() expression every time you want to query the data. That's going to make a lot more work for you on an ongoing basis than if you had stored the data normally.
Frankly, I have yet to see a question on Stack Overflow about using JSON with MySQL that wouldn't lead to the conclusion that storing data in relational tables is a better idea than using JSON, if the structure of the data doesn't need to vary.
You are approaching the task backwards.
Do the extraction as you insert the data. Insert into a small number of tables (Authors, Books, Tags, and maybe a couple more) and build relations between them. No JSON is needed in this database.
The result is an easy-to-query and fast database. However, it requires learning about RDBMS and SQL.
JSON is useful when the data is a collection of random stuff. Your JSON is very regular, hence the data fits very nicely into RDBMS technology. In that case, JSON is merely a standard way to serialize the data. But it should not be used for querying.
I've got several Postgres 9.4 tables that contain data like this:
| id | data |
|----|-------------------------------------------|
| 1 | {"user": "joe", "updated-time": 123} |
| 2 | {"message": "hi", "updated-time": 321} |
I need to transform the JSON column into something like this
| id | data |
|----|--------------------------------------------------------------|
| 1 | {"user": "joe", "updated-time": {123, "unit":"millis"}} |
| 2 | {"message": "hi", "updated-time": {321, "unit":"millis"}} |
Ideally it would be easy to apply the transformation to multiple tables. Tables that contain the JSON key data->'updated-time' should be updated, and ones that do not should be skipped. Thanks!
You can use the || operator to merge two jsonb objects together.
select '{"foo":"bar"}'::jsonb || '{"baz":"bar"}'::jsonb;
= {"baz": "bar", "foo": "bar"}
My Spark-SQL is generating an output for a query by joining two tables which has one to many cardinality. I have to convert the data into JSON.
This is how the output of the query will look like.
Address_id_parent | Address_id_child | Country_child | city_child
1 | 1 | India | Delhi
1 | 1 | US | NewYork
1 | 1 | US | NewJersey
The above data has to be converted to JSON in this way.
{
"Address": {
"Address_id_parent": "1"
},
"Address-details": [{
"Address_id_child": "1",
"location": [{
"country":"India",
"city":"Delhi",
},
{
"country":"US",
"city":"NewYork",
},
{
"country":"US",
"city":"NewJersey",
}
]
}]
}
How can I accomplish this?
Check Dataframe write interface with json:
df.write.format("json").save(path)