how to use Elasticsearch jsonBuilder() in java? - json

I have a document with the following schema,
{
"_index": "test",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"accountExpired": false,
"accountLocked": false,
"dateCreated": "2016-07-19T03:13:07Z",
"employee": {
"class": "bropen.framework.core.osm.Employee",
"id": 1
}
}
}
I want to modify the value of field "employee"
I have tried this:
String employee="\"{\"class\":\"bropen.framework.core.osm.Employee\",\"id\":1,\"birthdate\":null,\"code\":null,\"dateCreated\":\"2016-07-19T03:13:07Z\",\"degree\":null,\"disabled\":false,\"email\":null,\"entryDate\":null,\"graduateDate\":null,\"groups\":[],\"identities\":[{\"class\":\"bropen.framework.core.osm.EmployeeIdentity\",\"id\":1},{\"class\":\"bropen.framework.core.osm.EmployeeIdentity\",\"id\":33}],\"identityNumber\":null,\"lastUpdated\":\"2016-07-19T07:58:34Z\",\"level\":0.10,\"location\":null,\"mainIdentityId\":1,\"major\":null,\"mobile\":null,\"name\":\"张三\",\"nation\":null,\"nativePlace\":null,\"notes\":null,\"organization\":{\"class\":\"bropen.framework.core.osm.Organization\",\"id\":2},\"payrollPlace\":null,\"professionalRank\":null,\"qualification\":null,\"rank\":null,\"sequence\":10,\"sex\":null,\"syncId\":null,\"telephoneNumber\":null,\"title\":null,\"type\":1,\"user\":{\"class\":\"bropen.framework.core.security.User\",\"id\":2},\"workingDate\":null,\"workingYears\":null}\"";
try {
client.prepareUpdate("test", "user", "2")
.setDoc(jsonBuilder().startObject().field("testfield", employee).endObject()).get();
} catch (IOException e) {
e.printStackTrace();
}
return error info :
java.util.concurrent.ExecutionException: RemoteTransportException[[node-1][192.168.0.224:9300][indices:data/write/update]]; nested: RemoteTransportException[[node-1]
[192.168.0.224:9300][indices:data/write/update[s]]]; nested:
MapperParsingException[object mapping for [employee] tried to parse field [employee] as object, but found a concrete value];
at org.elasticsearch.common.util.concurrent.BaseFuture$Sync.getValue(BaseFuture.java:290)
how to use jsonBuilder() to build a json like this:
employee:{
"id":"1",
"class":"bropen",
"name":"Tom"
}
and modify the value of field "employee"?
--------------update------------------
I tried again:
public static void upMethod1() {
XContentBuilder json;
try {
json = jsonBuilder().startObject("employee").field("id", 1)
.field("class", "bropen").field("name", "Tom").endObject();
UpdateRequest request = new UpdateRequest();
request.index("test");
request.type("user");
request.id("2");
request.doc(json);
client.update(request).get();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
Error info:
java.util.concurrent.ExecutionException: RemoteTransportException[[node-1][127.0.0.1:9300][indices:data/write/update]]; nested: RemoteTransportException[[node-1][192.168.0.87:9300][indices:data/write/update[s]]]; nested: NotSerializableExceptionWrapper[not_x_content_exception: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes];
at org.elasticsearch.common.util.concurrent.BaseFuture$Sync.getValue(BaseFuture.java:290)

updating document is easy when you use json builder.
in your case inner object name is employee so you have to make object with name as "employee".
below is the code that helps you to understand the use of json builder.
XContentBuilder json = jsonBuilder()
.startObject("employee")
.field("id", 1)
.field("class", "bropen")
.field("name","Tom")
.endObject();
UpdateRequest request = new UpdateRequest();
request.index("test");
request.type("user");
request.id("2");
request.doc(json);
client.update(request).get();
you can see generated json using below code
String output = builder.string();

json = jsonBuilder().startObject()
.startObject("employee")
.field("id", 1)
.field("class", "bropen")
.field("name", "Tom")
.endObject()
.endObject();
UpdateRequest request = new UpdateRequest();
request.index("test");
request.type("user");
request.id("2");
request.doc(json);
System.out.println(json.toString());
client.update(request).get();

Related

How to decrypt a json response with gzip encoded data in flutter?

The Json response has encrypted objects which has to be decrypted to get the actual process. In android GZip was used .How can I achieve this The sample Json is as mentioned below.Any help is really appreciated.
{
"Data": "1.´ABCD´1150275,11028´01-Jan-2021´8,000.00´",
"Data": [
{
"Element": "8iMAAB+LCAAAAAAABADt1T8zBxwHgkefKcGh98Zcdz8FSqj9DMzK4d+L0Nj1tveNR2w6M8rRs3PJWBFDy"
},
{
"Element": "B1AV4bGp6JzQJI8ChnxzixrlT8vKnYHPwRM8zykKVn2gkceAFdxMwU0to"
}
],
"Status": 1,
"Msg": "Success",
"APIVersion": "1.4"
}
Basically how to decrypt a Gzip string. The same process was done in android ,but im new to flutter
Android java code is attached. i want to achieve something like that in flutter
public static String decompress(String zipText) throws IOException {
byte[] compressed = Base64.decode(zipText, Base64.DEFAULT);
if (compressed.length > 4) {
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressed, 4,compressed.length - 4));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int value = 0; value != -1; ) {
value = gzipInputStream.read();
if (value != -1) {
baos.write(value);
}
}
gzipInputStream.close();
baos.close();
return new String(baos.toByteArray(), StandardCharsets.UTF_8);
} else {
return "";
}
}
On way i tried was
List<int> data = utf8.encode(zipText);
var deCompressedString = GZipDecoder().decodeBytes(data);
print(deCompressedString);
Which throw exception
Unhandled Exception: FormatException: Invalid GZip Signature
For decrypt zipped: How to decode a Gzip Http Response in Flutter?
EDIT
decompress like this
String decompress(String zipText) {
final List<int> compressed = base64Decode(zipText);
if (compressed.length > 4) {
Uint8List uint8list = GZipDecoder().decodeBytes(compressed.sublist(4, compressed.length - 4));
// print( String.fromCharCodes(uint8list));
return String.fromCharCodes(uint8list);
} else {
return "";
}
}

On Unix server: unable to parse currency symbol(£,€) from json to .csv file

Below is my java code
public Test parseTest(String test) {
Testresult = null;
try {
result = gson.fromJson(test, Test.class);
if (CAT.isDebugEnabled()) {
CAT.debug(result);
}
} catch (JsonSyntaxException e) {
CAT.warn(e.getMessage() + "\nCan't parse\n" + test);
}
return result;
}
To parse Json I am using below jar
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
Below is my JSON:
Test": [
{
"A": "X;DOS533",
"B": "FCA BANK SPAEUR1.5BN21MAR2019",
"C": null,
"D": "AA BB EUR1.5BN",
"E": "€1.5BN Test LN BNK €100M 12M",
"Ccy": "EUR",
"TypeCode": "TML "
}
And below is the row from .csv file generated in unix box.
4243842|Test:ABC|Active||6||FFTIAIT||Internal|X;DOS5KT|FCA BANK SPAEUR1.5BN21MAR2019|?1.5BN Test LN NWM ?100M 12M|TML|
Here € sign get replace with ?(question mark).
Same issue i face while converting pound (£) from .csv to .bcp file.
I tried passing streams of bytes instead of String and it works.
Below is the updated code
public Test parseTest(String test) {
Testresult = null;
try {
result = gson.fromJson(new InputStreamReader(new ByteArrayInputStream(test.getBytes("UTF-8"))), Test.class);
if (CAT.isDebugEnabled()) {
CAT.debug(result);
}
} catch (JsonSyntaxException e) {
CAT.warn(e.getMessage() + "\nCan't parse\n" + test);
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}

UWP - From Json string to Structure (Classes)

I receive a JSon string from WS. It's so long that I can't use Json2charp to parse it and receive the structurated class.
I want to parse the string with a command. How is it possible?
I don't know the classes so I can't use a command like:
Dim result = JsonConvert.DeserializeObject(Of MyClass.RootObject)(String_From_File)
Is it possible from the string to obtain the class without using json2charp site ?
For example, in vs.net if on the variable 'string_from_file' I choose 'Json Visualizer' and see all classes and data in correct mode.
How can I obtain the same in my code ?
I have installed Newtonsoft.json
If you cannot use the json to class mappers like NewtonSoft.Json. You can use the Windows.Data.Json api. It let you parse and extract the data you want from your JSON string.
JsonValue jsonValue = JsonValue.Parse("{\"Width\": 800, \"Height\": 600, \"Title\": \"View from 15th Floor\", \"IDs\": [116, 943, 234, 38793]}");
double width = jsonValue.GetObject().GetNamedNumber("Width");
double height = jsonValue.GetObject().GetNamedNumber("Height");
string title = jsonValue.GetObject().GetNamedString("Title");
JsonArray ids = jsonValue.GetObject().GetNamedArray("IDs");
You can find a sample in the Windows Universal Sample GitHub.
A complex object parsing is shown here. I've extracted the most relevant parts here. The JSON string is provided to the User constructor which is extracting what it needs and then delegating the parsing to the nested School constructor.
{
"id": "1146217767",
"phone": null,
"name": "Satya Nadella",
"education": [
{
"school": {
"id": "204165836287254",
"name": "Contoso High School"
},
"type": "High School"
},
{
"school": {
"id": "116138758396662",
"name": "Contoso University"
},
"type": "College"
}
],
"timezone": -8,
"verified": true
}
This JSON fragment is parsed with this code:
public User(string jsonString) : this()
{
JsonObject jsonObject = JsonObject.Parse(jsonString);
Id = jsonObject.GetNamedString(idKey, "");
IJsonValue phoneJsonValue = jsonObject.GetNamedValue(phoneKey);
if (phoneJsonValue.ValueType == JsonValueType.Null)
{
Phone = null;
}
else
{
Phone = phoneJsonValue.GetString();
}
Name = jsonObject.GetNamedString(nameKey, "");
Timezone = jsonObject.GetNamedNumber(timezoneKey, 0);
Verified = jsonObject.GetNamedBoolean(verifiedKey, false);
foreach (IJsonValue jsonValue in jsonObject.GetNamedArray(educationKey, new JsonArray()))
{
if (jsonValue.ValueType == JsonValueType.Object)
{
Education.Add(new School(jsonValue.GetObject()));
}
}
}
public School(JsonObject jsonObject)
{
JsonObject schoolObject = jsonObject.GetNamedObject(schoolKey, null);
if (schoolObject != null)
{
Id = schoolObject.GetNamedString(idKey, "");
Name = schoolObject.GetNamedString(nameKey, "");
}
Type = jsonObject.GetNamedString(typeKey);
}
If you cannot use the automatic mapping from NewtonSoft.Json, you have no other way than doing it yourself.
Is not so simple.
The Json i received is very complicated and have many class
So i can't use
double width = jsonValue.GetObject().GetNamedNumber("Width");
Inside class i have more ...

How can I deserialize an invalid json ? Truncated list of objects

My json file is mostly an array that contain objects but the list is incomplete, so I can't use the last entry. I would like to deserialize the rest of the file while discarding the last invalid entry
[ { "key" : "value1" }, { "key " : "value2"}, { "key
Please tell me if there is a way using Newtonsoft.Json library, or do I need some preprocessing.
Thank you!
Looks like on Json.NET 8.0.3 you can stream your string from a JsonTextReader to a JTokenWriter and get a partial result by catching and swallowing the JsonReaderException that gets thrown when parsing the truncated JSON:
JToken root;
string exceptionPath = null;
using (var textReader = new StringReader(badJson))
using (var jsonReader = new JsonTextReader(textReader))
using (JTokenWriter jsonWriter = new JTokenWriter())
{
try
{
jsonWriter.WriteToken(jsonReader);
}
catch (JsonReaderException ex)
{
exceptionPath = ex.Path;
Debug.WriteLine(ex);
}
root = jsonWriter.Token;
}
Console.WriteLine(root);
if (exceptionPath != null)
{
Console.WriteLine("Error occurred with token: ");
var badToken = root.SelectToken(exceptionPath);
Console.WriteLine(badToken);
}
This results in:
[
{
"key": "value1"
},
{
"key ": "value2"
},
{}
]
You could then finish deserializing the partial object with JToken.ToObject. You could also delete the incomplete array entry by using badToken.Remove().
It would be better practice not to generate invalid JSON in the first place though. I'm also not entirely sure this is documented functionality of Json.NET, and thus it might not work with future versions of Json.NET. (E.g. conceivably Newtonsoft could change their algorithm such that JTokenWriter.Token is only set when writing is successful.)
You can use the JsonReader class and try to parse as far as you get. Something like the code below will parse as many properties as it gets and then throw an exception. This is of course if you want to deserialize into a concrete class.
public Partial FromJson(JsonReader reader)
{
while (reader.Read())
{
// Break on EndObject
if (reader.TokenType == JsonToken.EndObject)
break;
// Only look for properties
if (reader.TokenType != JsonToken.PropertyName)
continue;
switch ((string) reader.Value)
{
case "Id":
reader.Read();
Id = Convert.ToInt16(reader.Value);
break;
case "Name":
reader.Read();
Name = Convert.ToString(reader.Value);
break;
}
}
return this;
}
Code taken from the CGbR JSON Target.
the second answer above is really good and simple, helped me out!
static string FixPartialJson(string badJson)
{
JToken root;
string exceptionPath = null;
using (var textReader = new StringReader(badJson))
using (var jsonReader = new JsonTextReader(textReader))
using (JTokenWriter jsonWriter = new JTokenWriter())
{
try
{
jsonWriter.WriteToken(jsonReader);
}
catch (JsonReaderException ex)
{
exceptionPath = ex.Path;
}
root = jsonWriter.Token;
}
return root.ToString();
}

JSON.net string json into object

Hey all I am having difficultys in making this string JSON that I have into an object so that I can use it like this:
json("go")("to")("needed")("spot").toString
and get my value.
Currently the XML to JSON looks like this:
{
"?xml": {
"#version": "1.0",
"#encoding": "UTF-8"
},
"data": {
"recordCount": "1",
"metadata": {
"elementType": [
{
"name": "F_Paragraphtext",
"uiType": "textArea",
"dataType": "string",
"label": "Text String",
"dataLabel": ""
}
]
},
"records": {
"F_Form1": {
"#application_uid": "2a667c59-225c-4954-8e04-77bb083e5180",
"#uid": "61f876f4-9760-4013-85bb-37965cff1fb6",
"F_SingleLine": "test",
"F_Paragraphtext": "{\"Data\":{\"DevisionName\":\"testing service after added field\",\"DevisionCode\":\"test\",\"CodeInString\":\"test^test|4/15/2015|50%|test^test|4/15/2015|25%|test^test|4/23/2015|50%~test^test|4/23/2015|N/A|test^test|4/8/2015|N/A|test^test|3/31/2015|N/A~test^test|4/10/2015|N/A|test^test|5/1/2015|50%|test^test|4/18/2015|N/A~test^test|3/30/2015|50%|test^test|4/24/2015|50%|test^test|5/9/2015|100%~test^test|3/30/2015|25%|test^test|3/30/2015|100%|test^test|4/11/2015|75%\"}}"
}
}
}
}
And I am trying to get the value for F_Paragraphtext, #application_uid & #uid.
The VB.net code I have:
Dim doc1 As XmlDocument = New XmlDocument()
doc.LoadXml(serverResponse)
Dim json1 As String = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented)
jsreader = New Newtonsoft.Json.JsonTextReader(New StringReader(json1))
Try
json = DirectCast(New Newtonsoft.Json.JsonSerializer().Deserialize(jsreader), Newtonsoft.Json.Linq.JObject)
Catch ex As Exception
MsgBox(ex.Message)
End Try
For Each Row In json("data")("records")(febFormID)
Try
dName = json("#application_uid").ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
While Row value is:
{"#application_uid": "2a667c59-225c-4954-8e04-77bb083e5180"}
The error falls on the dName = Row("#application_uid").ToString. The error is Object reference not set to an instance of an object..
Not quite sure what all I am missing in order to be able to retrieve the Row values but apparently its something....
...making this string JSON that I have into an object so that I can use it like this:
json("go")("to")("needed")("spot").toString
I am trying to get the value for F_Paragraphtext, #application_uid & #uid.
Dim jstr As String = File.ReadAllText(filename)
Dim myJ = JObject.Parse(jstr)
Console.WriteLine("{0} == {1}", "F_Paragraphtext",
myJ("data")("records")("F_Form1")("F_Paragraphtext"))
Console.WriteLine("{0} == {1}", "#application_uid",
myJ("data")("records")("F_Form1")("#application_uid"))
Console.WriteLine("{0} == {1}", "#uid",
myJ("data")("records")("F_Form1")("#uid"))
Output:
F_Paragraphtext == {"Data":{"DevisionName":"testing service after added field","DevisionCode":"test","CodeInString":"test^test|4/15/2015|50%|test^test|4/15/2015|25%|test^test|4/23/2015|50%~test^test|4/23/2015|N/A|test^test|4/8/2015|N/A|test^test|3/31/2015|N/A~test^test|4/10/2015|N/A|test^test|5/1/2015|50%|test^test|4/18/2015|N/A~test^test|3/30/2015|50%|test^test|4/24/2015|50%|test^test|5/9/2015|100%~test^test|3/30/2015|25%|test^test|3/30/2015|100%|test^test|4/11/2015|75%"}}
#application_uid == 2a667c59-225c-4954-8e04-77bb083e5180
#uid == 61f876f4-9760-4013-85bb-37965cff1fb6