I try to index json data to solr.
SolrClient solr = new HttpSolrClient.Builder(SOLR_URL).build();
SolrInputDocument document = new SolrInputDocument();
String stringToParse = "{\"id\": \"101\",\"name\": \"Baia Otel\",\"services\":[{\"room_id\":\"1234\",\"service_name\":\"Shuttle\"},{\"room_id\":\"1235\",\"service_name\":\"Accommodation\"}]}";
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(stringToParse);
document.addField("info", json.toJSONString());
} catch (ParseException e) {
e.printStackTrace();
}
On document.addField method if I use json.toJSONString() it indexes json data as string, and when I just use jsonObject without casting it to JSONString;
document.addField("info",json)
it does not index this field.
How can I index json to solr so that it will be indexed nested?
You can find examples of indexing JSON with Solrj for schemaless Solr in this blog.
Related
I'm trying to make a program using json to save user registers to file, but when I try to read my json I get the error "JSON malformed". This is my code to write in json:
Gson gsonObject = new Gson();
FileWriter writerFile;
try {
writerFile = new FileWriter("users.json", true);
writerFile.write(gsonObject.toJson(writerUser));
writerFile.close();
} catch (IOException e) {
e.printStackTrace();
}
I use arrays to be easy modify the values (only take the json back to array and make my modify) but i can't reader this json file after writer, this is my reader code:
Gson gson = new Gson();
List<User> userList;
try {
BufferedReader readerUserList = new BufferedReader(new FileReader("users.json"));
userList = Arrays.asList(gson.fromJson(readerUserList, User[].class));
for (User user : userList) {
System.out.println(user.toString());
}
And after that i get "json malformed" error, where is my error?
If help you guys, inside my json file is my arrays like that:
[{"name":"james","idCode":1}][{"name":"kyle","idCode":2}][{"name":"test","idCode":3}]
I've never used Gson... but your json file has format problems, it is supposed to look like this:
[{"name":"james","idCode":1},{"name":"kyle","idCode":2},{"name":"test","idCode":3}]
The problem is not when you try to read, but when you write it.
I'm getting JSON string as an input,
I converted the Json string to avro schema using this
Schema schema = JsonUtil.inferSchema(JsonUtil.parse(jsonString),
"schema");
I have the avro schema which I can register in schema registry.
I need a genric record out of the same string, as the JSON data holds the values as well.
I used
tech.allegro.schema.json2avro.converter
Now I need to pass schema and the json data(as bytes) and this would give me the record.
JsonAvroConverter avroConverter = new JsonAvroConverter();
GenericData.Record record =
avroConverter.convertToGenericDataRecord(json.getBytes(), avroSchema);
Sounds like you are trying to programmatically re-create the kafka-avro-console-producer which takes the schema as a CLI argument, then JSON matching the schema as user input.
You can find a corresponding jsonToAvro method there
https://github.com/confluentinc/schema-registry/blob/master/avro-serializer/src/main/java/io/confluent/kafka/formatter/AvroMessageReader.java
Which just uses Avro's included JSONDecoder
private Object jsonToAvro(String jsonString, Schema schema) {
try {
DatumReader<Object> reader = new GenericDatumReader<Object>(schema);
Object object = reader.read(null, decoderFactory.jsonDecoder(schema, jsonString));
if (schema.getType().equals(Schema.Type.STRING)) {
object = ((Utf8) object).toString();
}
return object;
} catch (IOException e) {
throw new SerializationException(
String.format("Error deserializing json %s to Avro of schema %s", jsonString, schema), e);
} catch (AvroRuntimeException e) {
throw new SerializationException(
String.format("Error deserializing json %s to Avro of schema %s", jsonString, schema), e);
}
}
This returns a String or a GenericRecord instance
I have json structure like as shown in image. How should I use jsonparser for this?
Use like this
String data;//your entire JSON data as String
try {
JSONObject object = new JSONObject(data);
String status = object.getString("status");
String response = object.getString("response");
} catch (JSONException e) {
e.printStackTrace();
}
Create Model class like your JSON
and use GSON library to parse your json into model.
I might be getting a bit tired tonight but here it goes:
I'd like to have GWT HashMap to/from JSON. How would I achieve this?
In other words, I'd like to take an HashMap, take its JSON representation, store it somewhere and get it back to its native Java representation.
Here is my quick solution:
public static String toJson(Map<String, String> map) {
String json = "";
if (map != null && !map.isEmpty()) {
JSONObject jsonObj = new JSONObject();
for (Map.Entry<String, String> entry: map.entrySet()) {
jsonObj.put(entry.getKey(), new JSONString(entry.getValue()));
}
json = jsonObj.toString();
}
return json;
}
public static Map<String, String> toMap(String jsonStr) {
Map<String, String> map = new HashMap<String, String>();
JSONValue parsed = JSONParser.parseStrict(jsonStr);
JSONObject jsonObj = parsed.isObject();
if (jsonObj != null) {
for (String key : jsonObj.keySet()) {
map.put(key, jsonObj.get(key).isString().stringValue());
}
}
return map;
}
Not the most optimized, but should be easy to code: use JSONObject.
Iterate over your map's entries and put them in a JSONObject (converting each value to a JSONValue of the appropriate type), then call toString to get the JSON representation.
For parsing, get a JSONObject back using a JSONParser, then iterate over the keySet, geting the values and putting them in your map (after unwrapping the JSONValues)
But beware of the keys you use! You cannot use any kind of key as a property name in JS; and JSON processing in the browser always involve going through a JS object (or implementing the JSON parser yourself, which won't perform the same)
I am writing code that needs to extract an object literal posted to a servlet. I have studied the API for the HttpServletRequest object, but it is not clear to me how to get the JSON object out of the request since it is not posted from a form element on a web page.
Any insight is appreciated.
Thanks.
are you looking for this ?
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuilder sb = new StringBuilder();
BufferedReader reader = request.getReader();
try {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} finally {
reader.close();
}
System.out.println(sb.toString());
}
This is simple method to get request data from HttpServletRequest
using Java 8 Stream API:
String requestData = request.getReader().lines().collect(Collectors.joining());
make use of the jackson JSON processor
ObjectMapper mapper = new ObjectMapper();
Book book = mapper.readValue(request.getInputStream(),Book.class);
The easiest way is to populate your bean would be from a Reader object, this can be done in a single call:
BufferedReader reader = request.getReader();
Gson gson = new Gson();
MyBean myBean = gson.fromJson(reader, MyBean.class);
There is another way to do it, using org.apache.commons.io.IOUtils to extract the String from the request
String jsonString = IOUtils.toString(request.getInputStream());
Then you can do whatever you want, convert it to JSON or other object with Gson, etc.
JSONObject json = new JSONObject(jsonString);
MyObject myObject = new Gson().fromJson(jsonString, MyObject.class);
If you're trying to get data out of the request body, the code above works. But, I think you are having the same problem I was..
If the data in the body is in JSON form, and you want it as a Java object, you'll need to parse it yourself, or use a library like google-gson to handle it for you. You should look at the docs and examples at the project's website to know how to use it. It's fairly simple.
Converting the retreived data from the request object to json object is as below using google-gson
Gson gson = new Gson();
ABCClass c1 = gson.fromJson(data, ABCClass.class);
//ABC class is a class whose strcuture matches to the data variable retrieved