Delete element in JSONPath Java - java

I have a JSON file like this:
{
"objects": [{
"type": "FirstType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheFirstType",
"font": "18px arial"
},
"id": "18e"
},
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
My goal is to delete nodes of a certain type and id in details F.e. if I would like to delete elements of type named FirstType and id of 1.I would get:
{
"objects": [
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
I think I partialy achieved this:
final DocumentContext jsonContext = JsonPath.parse(element.getJsonContent());
jsonContext.delete("$['objects'][?(#.type == 'FirstType')][?(#.details.id == '1')]");
But I would like to consider type and id in details as well, but I am not sure if two filter expressions are written correctly. I feel like I stuck here
EDITED: Solved
Ok. For future references, correct form goes like this:
DocumentContext jsonContext = JsonPath.parse(element.getJsonContent());
jsonContext.delete("$['objects'][?(#.type == 'FirstType' && #.details.id == 1)]");
element.setJsonContent(jsonContext.jsonString());

Related

how to index each feature elasticsearch featurecollection

I got a typical FeatureCollection json file, when I import this file via Kibana I can see that the index has docs count equivalent to a number of features in FeatureCollection, so I can do a query and get the specific feature.
But when I try to index this file via java and using mapping like this, I can save the file but with docs count = 1, so I can't do geospatial query to get a specific feature.
{
"mappings": {
"dynamic": true,
"properties": {
"checksum": {
"type": "keyword"
},
"geometry": {
"properties": {
"coordinates": {
"type": "geo_shape"
}
}
}
}
}
}
this is the file:
{
"hashCode": 1708148999,
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "station_exits.fid--3e26eb90_1774de53429_-64b5",
"geometry": {
"type": "Point",
"coordinates": [38.5752041, 54.8366001, 75.25849601491346]
},
"properties": {
"gid": 1,
"obj_id": "004528ca-3b1f-4210-b10e-afab2d268144",
"prefect": "SAO",
"district": "Timit",
"obj_name": "exits",
"line": "xxxxx",
"status": "xxxx",
"link": "/view/main"
}
}
]
}
Can anyone help to make the right mapping?

Elasticsearch nested sort - mismatch between document and nested object used for sorting

I've been developing a new search API with AWS Elasticsearch (version 6.2) as backend.
Right now, I'm trying to support "sort" options for the API.
My mapping is as follows (unrelated fields not included):
{
"properties": {
"id": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"description": {
"type": "text"
},
"materialDefinitionProperties": {
"type": "nested",
"properties": {
"id": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
},
"analyzer": "case_sensitive_analyzer"
},
"value" : {
"type": "nested",
"properties": {
"valueString": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
I'm attempting to allow the users sort by property value (path: materialDefinitionProperties.value.valueLong.raw).
Note that it's inside 2 levels of nested objects (materialDefinitionProperties and materialDefinitionProperties.value are nested objects).
To sort the results by the value of property with ID "PART NUMBER", my request for sorting is:
{
"fieldName": "materialDefinitionProperties.value.valueString.raw",
"nestedSort": {
"path": "materialDefinitionProperties",
"filter": {
"fieldName": "materialDefinitionProperties.id",
"value": "PART NUMBER",
"slop": 0,
"boost": 1
},
"nestedSort": {
"path": "materialDefinitionProperties.value"
}
},
"order": "ASC"
}
However, as I examined the response, the "sort" field does not match with document's property value:
{
"_index": "material-definition-index-v2",
"_type": "default",
"_id": "development_LITL4ZCNE",
"_source": {
"id": "LITL4ZCNE",
"description": [
"CPU, Intel, Cascade Lake, 8259CL, 24C, 210W, B1 Prod"
]
"materialDefinitionProperties": [
{
"id": "PART NUMBER",
"description": [],
"value": [
{
"valueString": "202-001193-001",
"isOriginal": true
}
]
}
]
},
"sort": [
"100-000018"
]
},
The document's PART NUMBER property is "202-001193-001", the "sort" field says "100-000018", which is the part number of another document.
It seems that there's a mismatch between the master document and nested object used for sorting.
This request worked well when there's only a small number of documents in the cluster. But once I backfill the cluster with ~1 million of records, the symptom appears. I've also tried creating a new ES cluster but the results are the same.
Sorting by other non-nested attributes worked well.
Did I misunderstand the concept of nested objects, or misuse the nested sort feature?
Any ideas appreciated!
This is a bug in Elasticsearch. Upgrading to 6.4.0 fixed the issue.
Issue tracker: https://github.com/elastic/elasticsearch/pull/32204
Release note: https://www.elastic.co/guide/en/elasticsearch/reference/current/release-notes-6.4.0.html

How to parse a DiagnosticReport from a JSON and print the same JSON again?

I'm parsing a DiagnosticReport from a JSON file and It works fine, but when I try to print the same JSON file throught IParser encode function, the JSON is different to the original. I need to print the same JSON.
Original JSON (String json)
{
"resourceType": "DiagnosticReport",
"text": {
"status": "generated",
"div": "<div><p><b>Narrative A</b></p></div>"
},
"contained": [
{
"resourceType": "Patient",
"id": "1"
},
{
"resourceType": "Observation",
"id": "2",
"meta": {
"lastUpdated": "2017-03-22T22:00:28.089-05:00"
},
"text": {
"div": "<div><p><b>Narrative B</b></p></div>"
},
"comment": "a comment"
}
],
"status": "appended",
"code": {
"coding": [
{
"code": "Report01"
}
]
},
"subject": {
"reference": "#1"
},
"effectiveDateTime": "2017-03-22T22:00:28-05:00",
"issued": "2017-03-22T22:00:28.070-05:00",
"result": [
{
"reference": "#2"
}
]
}
First step is parse and the second step is encode and print
DiagnosticReport report = parser.parseResource(DiagnosticReport.class, json);
String encodeJSON = parser.encodeResourceToString(report);
System.out.println(encodeJSON);
And the result is different because the text tag in the Observation is not showed
{
"resourceType": "DiagnosticReport",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Narrative A</b></p></div>"
},
"contained": [
{
"resourceType": "Patient",
"id": "1"
},
{
"resourceType": "Observation",
"id": "2",
"meta": {
"lastUpdated": "2017-03-22T22:00:28.089-05:00"
},
"comment": "a comment"
}
],
"status": "appended",
"code": {
"coding": [
{
"code": "Report01"
}
]
},
"subject": {
"reference": "#1"
},
"effectiveDateTime": "2017-03-22T22:00:28-05:00",
"issued": "2017-03-22T22:00:28.070-05:00",
"result": [
{
"reference": "#2"
}
]
}
I'm trying this because I have a DiagnosticReport generated by my software and I need print it completely in a JSON file.
Thanks for your help!!
It's not legal to have narrative in a contained resource, nor is it legal to have meta/lastUpdated. There are invariants that prohibit both. Ideally, the parsing software should have thrown an exception, but it's not overly surprising that the serializer has trouble serializing content that's not supposed to be there.
Look at dom-1 and dom-4 in dstu3 or dstu2

How do you map Json structure having type identification inside the node

{
"task": {
"vendorConnectorId": 9901,
"operation": "query",
"objectType": "",
"attributes": {
"q": "SELECT Name FROM Asset where AccountId = '00128000005O5uPAAS'"
}
},
"httpStatusCode": 200,
"resultSet": {
"result": {
"totalSize": 1,
"records": [
{
"attributes": {
"type": "Asset",
"url": "/services/data/v34.0/sobjects/Asset/02i280000007BcpAAE"
},
"Name": "Flight To LA"
}
],
"done": true
}
},
"messages": [],
"startedTime": 1441969739375,
"endTime": 1441969750317
}
I want to map records node via polymorphic de-serialization feature of Jackson but type information is nested inside the record node(attribute node).

generating POJOs from JSON Schema for non-object types

I am trying to generate POJOs from the JSON Schema of XMBC.
I do this with jsonschema2pojo.
However, nothing gets generated. It doesn't even bring me an error.
This is a reduced sample json schema I am trying to generate from:
{
"description": "JSON-RPC API of XBMC",
"id": "http://xbmc.org/jsonrpc/ServiceDescription.json",
"methods": {
"Addons.ExecuteAddon": {
"description": "Executes the given addon with the given parameters (if possible)",
"params": [
{
"name": "addonid",
"required": true,
"type": "string"
},
{
"default": "",
"name": "params",
"type": [
{
"additionalProperties": {
"default": "",
"type": "string"
},
"type": "object"
},
{
"items": {
"type": "string"
},
"type": "array"
},
{
"description": "URL path (must start with / or ?",
"type": "string"
}
]
},
{
"default": false,
"name": "wait",
"type": "boolean"
}
],
"returns": {
"type": "string"
},
"type": "method"
}
},
"notifications": {
"Application.OnVolumeChanged": {
"description": "The volume of the application has changed.",
"params": [
{
"name": "sender",
"required": true,
"type": "string"
},
{
"name": "data",
"properties": {
"muted": {
"required": true,
"type": "boolean"
},
"volume": {
"maximum": 100,
"minimum": 0,
"required": true,
"type": "integer"
}
},
"required": true,
"type": "object"
}
],
"returns": null,
"type": "notification"
}
},
"types": {
"Addon.Content": {
"default": "unknown",
"enums": [
"unknown",
"video",
"audio",
"image",
"executable"
],
"id": "Addon.Content",
"type": "string"
}
},
"version": "6.14.3"
}
I must admin that my knowledge of JSON is very terse, maybe it is just a simple fault of mine. But can anyone help me how I can generate Java objects from such a JSON Schema?
The JSON Schema doesn't support method. JSON schema defines json data structure, it would not be used to define your methods. Most important attribute in JSON schema is properties.
It's good to generate POJO data models from a JSON schema, but not business logic. You can learn the JSON schema from those examples.

Categories