You can import the json into a table with a single row and one column and use the built in json functions to extract the values you need.
Based on the given SQL
SELECT DISTINCT id FROM table WHERE title = "test" AND id = 42
and the provided foo
object
{"title": "test", "id": 42}
I assume that you have an array of json objects like this
[
{"title": "test 1", "id": 42},
{"title": "test 2", "id": 43}
]
In my case i had a pmd json output with an array of json objects ([finding-1, finding-2]
). Each array element finding
contains another json object code
which contains another json object target
array : [
finding-1 { ... }
|
|--- code : { ... }
|
|--- target { ... }
,finding-2 { ... }
,finding-3 { ... }
]
similar to this
[
{"resource": "/C:/src/one.cls",
"code": {
"value": "AvoidGlobalModifier",
"target": {
"path": "/latest/pmd_rules_apex_bestpractices.html",
"fragment": "avoidglobalmodifier"
}
},
"severity": 4,
"message": "Avoid using global modifier ..."
},
{"resource": "/C:/src/two.cls",
"code": {
"value": "ApexSOQLInjection",
"target": {
"path": "/latest/pmd_rules_apex_bestpractices.html",
"fragment": "apexsoqlinjection"
}
},
"severity": 4,
"message": "... escape variables merged in DML query ..."
}]
I created table pmd_report
where i copied the json-array into the column pmd_output
in a single record. The table looks like this
CREATE TABLE "pmd_report" (
"id" INTEGER,
"import_date" TEXT,
"pmd_output" TEXT
)
To extract the values from the json array and the json objects this SQL statement can be used
SELECT
json_extract(json_parsed, '$.code.target.fragment') as code_target_fragment,
json_extract(json_parsed, '$.severity') as severity,
json_extract(json_parsed, '$.message') as message,
json_extract(json_parsed, '$.resource') as resource,
json_extract(json_parsed, '$.code.value') as code_value,
json_extract(json_parsed, '$.code.target.path') as code_target_path
FROM (
SELECT jsonEach.Value as json_parsed
FROM pmd_report,json_each(pmd_report.pmd_output, '$') as jsonEach
) as subquery;
To store the extracted json you can insert the rows into a new table like this
INSERT INTO pmd_report_2 (fragment, severity, message, startLineNumber, endLineNumber,
resource,
code_value,
code_target_path
)
SELECT code_target_fragment, severity, message,
startLineNumber, endLineNumber, substr(resource, 43) as resource,
code_value, code_target_path
FROM (
SELECT
json_extract(json_parsed, '$.code.target.fragment') as code_target_fragment,
json_extract(json_parsed, '$.severity') as severity,
json_extract(json_parsed, '$.message') as message,
json_extract(json_parsed, '$.startLineNumber') as startLineNumber,
json_extract(json_parsed, '$.endLineNumber') as endLineNumber,
json_extract(json_parsed, '$.resource') as resource,
json_extract(json_parsed, '$.code.value') as code_value,
json_extract(json_parsed, '$.code.target.path') as code_target_path
FROM (
SELECT jsonEach.Value as json_parsed
FROM pmd_report,json_each(pmd_report.pmd_output, '$') as jsonEach
) as subquery )
as subquery_2;