I have a JSONObject which inturn contains two JSONObjects ( key is rows_map and columns_map)
{
"rows_map":{
"3":["Test","Test","Test","Test","Test",null,null,null,null,null,"2011-10-07 15:47:56.0",null,null],
"2":["test","","","","",null,null,null,"123456789","123456789.user","2011-10-07 12:49:49.0",null,null]
},
"columns_map":{
columns_map":"fld1","fld2","fld3","fld4","fld5","Latitude","Longitude","Altitude","Mobile Number","Name","Time","Message","Advertisment"]
}
}
In rows_map 3 and 2 are record numbers.
Each record is related to columns in columns_map.
I want to get records list based on mobile number column
for eg: recordslist of mobilenumber equal to 123456789
How can i do this.
In the case that you're trying to read a json using java, you should be parsing it as an object rather than via syntax. Take a look at:
https://stackoverflow.com/questions/338586/a-better-java-json-library
Related
I have 'a unique-id'. I want to fetch records from table on basis of that unique-id. I have a column named "request body" that contains a nested json string which is of type text. Is there any way i can compare 'unique-id' with the 'unique-id' inside the json string cloumn i-e request body?
Apologies, I am new to stackoverflow.
For anyone looking for the solution, below are the two approaches:
APPROACH 1
SELECT t.request_body
FROM table t
WHERE cast(request_body as JSON) ->> 'uniqueId' ='123'
APPROACH 2
SELECT t.request_body
FROM table t
WHERE (substr(t.request_body, position ('uniqueId' in request_body)+11,3) ='123'
Note: 11 represents lenght of 'uniqueId":"' and 3 for the following id 123
I want to write/append data to a CSV file, column-by-column, in below fashion:
query1 query2 query3
data_item1 data_item7 data_item12
data_item2 data_item8 data_item13
data_item3 data_item9 data_item14
data_item4 data_item10
data_item5 data_item11
data_item6
I have the data in a hashMap, with the queryID (i.e. query1,query2) being the key and data_items for the
corresponding queries being the values.
The values(data_items for every query) are in a list.
Therefore, my hash map looks like this :
HashMap<String,List<String>> hm = new HashMap<String,List<String>>();
How can I write this data, column by column to a csv, as demonstrated above, using JAVA ?
I tried CSVWriter, but couldn't do it. Can anyone please help me out ?
csv files are mostly used to persist data structured like a table... meaning data with columns and rows that are in a close context.
In your example there seems to be only a very loose connection between query1, 2 and 3, and no connection horizontally between item 1,7 and 12, or 2, 8 and 13 and so on.
On top of that writing into files are usually facilitated along rows or lines. So you open your file write one line, and then another and so on.
So to write the data columnwise as you are asking, you have to either restructure your data in your code alrady to have all the data which is written into one line available on writing that line, or run through your csv file and it's lines several times, each time adding another item to a row. Of course the latter option is very time consuming and would not make much sense.
So i would suggest if there is really no connection between the data of the 3 queries, you either write your data into 3 different csv files: query1.csv, 2.csv and 3.csv.
Or, if you have a horizontal connection i.e. between item 1,7 and 12, and so on you write it into one csv file, organizing the data into rows and columns. Something like:
queryNo columnX columnY columnZ
1 item1 item2 item3
2 item7 item8 item9
3 item12 item13 item14
How to do that is well described in this thread: Java - Writing strings to a CSV file.
Other examples you can also find here https://mkyong.com/java/how-to-export-data-to-csv-file-java/
After days of tinkering around, I finally succeeded. Here is the implementation :
for(int k=0;k<maxRows;k++) {
List<String> rowValues = new ArrayList<String>();
for(int i=0;i<queryIdListArr.length;i++) {
subList = qValuesList.subList(i, i+1);
List<String> subList2 = subList.stream().flatMap(List::stream).collect(Collectors.toList());
if(subList2.size()<=k) {
rowValues.add("");
}else{
rowValues.add(subList2.get(k));
}
}
String[] rowValuesArr = new String[rowValues.size()];
rowValuesArr = rowValues.toArray(rowValuesArr);
// System.out.println(rowValues);
writer.writeNext(rowValuesArr);
}
maxRows : Size of the value list with max size. I have a list of values for each key. My hash map looks like this
HashMap<String,List<String>> hm = new HashMap<String,List<String>>();
queryIdListArr : List of all the values obtained from the hash map.
qValuesList : List of all the value lists.
List<List<String>> qValuesList = new ArrayList<List<String>>();
subList2 : sublist obtained from qValuesList using the below syntax :
qValuesList.subList(i, i+1);
rowValuesArr is an array that gets populated with the index wise value for each
value fetched from qValuesList.
The idea is to fetch all the values for each index from all the sublists and then write those values to the row. If for that index, no value is found, write a blank character.
Guide I'm working with: http://crunchify.com/how-to-write-json-object-to-file-in-java/
I've currently created an application which downloads a big JSON file and parses the data which list items into it, but unfortunately all items are named as it's ID so basically it means that I have to fetch all of the item ID's from separate files, because their JSON doesn't support querying a many items once i.e "item=1983,1093,984,2847" so I have do it for EVERY item individually.
That means I have to make many query connections to remote service in order to find names of up to 35,000 items which isn't very good choice since my query limit will reach to maximum in a seconds.
I want to create a custom JSON file that the item ids and names are fetched into it so it won't use anyone's bandwidth in order to gain item name.
So I'm thinking about 2D array, it's like [int][int] so what is this called for and how do I make my Java JSON (json-simple-1.1.1) understand that the item id will be the (object?) and rest info that relates to that id will be (key?)
{
"1094" // Item ID
{
"name": "item name",
"info": "item info"
}
}
In PHP there was something similar like this:
foreach($whatever as $something => $key) {
}
Item ID must be the identifier that I can parse rest of the details from it.
Edit: So "item id" should be JSONArray which is placed to object so I can add "keys" to it? I'm not sure about the terms.
I am trying to use the datastax java driver and retrieve the row as a JSON.
I do the classic
SELECT JSON * from myTable WHERE id=1 and this returns a Json formatted string on CQL.
e.g { "uuid" : "12324567-...." }
This works.
Now when, I try to do the same use the Java driver, I use (in scala)
val resultSet = session.execute(queryString)
I pick up one row from this result set using: "resultSet.one()".
This has the string I need, but how do I pick this up?
Experiment: resultSet.one().getColumnDefinitions.toString
Prints: Columns[ [json] (varchar) ]
Experiment: resultSet.one().toString()
Prints: Row[{"uuid": "3ce19e07-2280-4b31-9475-992bda608e70"}] <- String I need
How do I pick up a simple string that represents the JSON in my program, without trying to split the strings above ?
As noted in the the Cassandra documentation:
The results for SELECT JSON will only include a single column named [json]. This column will contain the same JSON-encoded map representation of a row that is used for INSERT JSON.
In order to access the JSON value of the returned row, you need to use one of the getString methods defined on the Row class to get the value of this column either by index or by name:
Row row = resultSet.one();
String json1 = row.getString(0);
String json2 = row.getString("[json]");
I have a cart array (JavaScript array) in my web application where I store different products. And each product has different specifications (i.e. URL, name, price, quantity, total price) which is again stored in an array.
So it forms an array of arrays. I need to save this cart to a database.
To persist it to database I need to get these arrays in a Java class and then save to database.
How can I take this array to database?
My array which is in json format goes goes like this (dummy array):
{"products":[{"productURL":"images/product/a.jpg","productName":"Headfones 1","productPrice":"234","productQuantity":"2","productTotal":"468"},
{"productURL":"images/product/b.jpg","productName":"Headfones 2","productPrice":"234","productQuantity":"3","productTotal":"702"},
{"productURL":"images/product/d.jpg","productName":"Headfones 4","productPrice":"234","productQuantity":"1","productTotal":"234"},
{"productURL":"images/product/d.jpg","productName":"Headfones 4","productPrice":"234","productQuantity":"6","productTotal":"1404"}]}
any library for parsing json??
Send the JavaScript array in JSON format to the server. Then parse JSON on the server and convert to a Java array. I'd recommend the Jackson JSON parser. This is a working example:
String JSON = "[[\"images/product/a.jpg\",\"Headfones 1\",\"234\",3,702],"
+ "[\"images/product/b.jpg\",\"Headfones 2\",\"234\",4,936],"
+ "[\"images/product/c.jpg\",\"Headfones 3\",\"234\",5,1170]]";
String[][] products = new ObjectMapper().readValue(
JSON,
new TypeReference<String[][]>() {});
With the products array, you can now start writing the data to the database.