[
target {
column: "marks"
}
parents {
table {
kind: BIGQUERY_TABLE
sqlResource: "bigquery.table.location"
}
}
,
target {
column: "name"
}
parents {
table {
kind: BIGQUERY_TABLE
sqlResource: "bigquery.table.location"
}
}
]
It is an array like input (comma separated) and I want to read the column(as specified under 'target:column' from a bigquery table (location of bqtable in 'parent:table:sqlResource' and store them in a key-value pair. How should it be done? I tried it using if-else conditions and I feel there should be a efficient way to do this? Please let me know a better solution.
CodePudding user response:
There are two parts in your question:
- How to parse the String you provided
- How to used parsed info
I will answer the first part. The String you provided looks like a JSON string, but it is an invalid JSON. After keys "target", "parents" and "table" there must be symbol ":". That will make it a valid JSON. Assuming that you have this. You can parse your JSON using any of available JSON libraries (notably Json-Jackson, (Maven artifacts here) or Gson). I wrote my own open source library called MgntUtils, that has JSON parser based on Json-Jackson. Using this library you can easily parse your JSON String into List of Maps
List<Map<String, Object>> list = null;
try {
list = JsonUtils.readObjectFromJsonString(jsonString, List.class);
}
} catch (IOException e) {
...
}
Your list will contain two Map elements where under the key "target" you will have a map with key "column" and its value and so forth. Here is the relevant Javadoc. The MgntUtils Maven artifacts could be found here and Github project with source code and javadoc is here
