My program currently gets a response from an api which returns an extensive json script, usually around 400 lines or more. Within this JSON there are well over 20 elements, of which only about 2 are needed for other functions, the rest just needs to printed.
I have tried to make classes for each element but it became excessive, especially when the elements had different variations, and so i was told a recursive approach would be better suited. My current attempt involves using gson, although I find myself using instanceof in order to determine whether the next json element can be mapped using gson.
My JSON script looks as follows:
{
"id": "fox",
"metadata": {
"operation": "retrieve",
"provider": "Oxford University Press",
"schema": "RetrieveEntry"
},
"results": [
{
"id": "fox",
"language": "en-gb",
"lexicalEntries": [
{
"derivatives": [
{
"id": "foxlike",
"text": "foxlike"
}
],
"entries": [
{
"etymologies": [
"Old English, of Germanic origin; related to Dutch vos and German Fuchs"
],
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"a carnivorous mammal of the dog family with a pointed muzzle and bushy tail, proverbial for its cunning."
],
"domainClasses": [
{
"id": "mammal",
"text": "Mammal"
}
],
"id": "m_en_gbus0384950.006",
"notes": [
{
"text": "Vulpes and three other genera, family Canidae: several species, including the red fox and the arctic fox",
"type": "technicalNote"
}
],
"semanticClasses": [
{
"id": "canid",
"text": "Canid"
}
],
"shortDefinitions": [
"carnivorous mammal"
],
"subsenses": [
{
"definitions": [
"the fur of a fox."
],
"domainClasses": [
{
"id": "mammal",
"text": "Mammal"
}
],
"id": "m_en_gbus0384950.010",
"notes": [
{
"text": "mass noun",
"type": "grammaticalNote"
}
],
"semanticClasses": [
{
"id": "fur",
"text": "Fur"
}
],
"shortDefinitions": [
"fur of fox"
]
}
],
"synonyms": [
{
"language": "en",
"text": "Reynard"
}
],
"thesaurusLinks": [
{
"entry_id": "fox",
"sense_id": "t_en_gb0006011.001"
}
]
},
{
"definitions": [
"a cunning or sly person"
],
"examples": [
{
"text": "a wily old fox"
}
],
"id": "m_en_gbus0384950.013",
"semanticClasses": [
{
"id": "disliked_person",
"text": "Disliked_Person"
}
],
"shortDefinitions": [
"cunning or sly person"
]
},
{
"definitions": [
"a sexually attractive woman."
],
"id": "m_en_gbus0384950.015",
"regions": [
{
"id": "north_american",
"text": "North_American"
}
],
"registers": [
{
"id": "informal",
"text": "Informal"
}
],
"semanticClasses": [
{
"id": "attractive_female",
"text": "Attractive_Female"
}
],
"shortDefinitions": [
"sexually attractive woman"
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "noun",
"text": "Noun"
},
"text": "fox"
},
{
"derivatives": [
{
"id": "foxlike",
"text": "foxlike"
}
],
"entries": [
{
"grammaticalFeatures": [
{
"id": "transitive",
"text": "Transitive",
"type": "Subcategorization"
}
],
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"baffle or deceive (someone)"
],
"examples": [
{
"text": "the abbreviation foxed me completely"
}
],
"id": "m_en_gbus0384950.017",
"registers": [
{
"id": "informal",
"text": "Informal"
}
],
"shortDefinitions": [
"baffle or deceive"
],
"subsenses": [
{
"definitions": [
"behave in a cunning or sly way"
],
"examples": [
{
"text": "to his mind everybody was dodging and foxing"
}
],
"id": "m_en_gbus0384950.023",
"notes": [
{
"text": "no object",
"type": "grammaticalNote"
}
],
"registers": [
{
"id": "dated",
"text": "Dated"
}
],
"shortDefinitions": [
"behave in cunning or sly way"
]
}
],
"synonyms": [
{
"language": "en",
"text": "baffle"
},
{
"language": "en",
"text": "bewilder"
},
{
"language": "en",
"text": "mystify"
},
{
"language": "en",
"text": "bemuse"
},
{
"language": "en",
"text": "perplex"
},
{
"language": "en",
"text": "puzzle"
},
{
"language": "en",
"text": "confuse"
},
{
"language": "en",
"text": "confound"
},
{
"language": "en",
"text": "nonplus"
},
{
"language": "en",
"text": "disconcert"
},
{
"language": "en",
"text": "throw"
},
{
"language": "en",
"text": "throw off balance"
},
{
"language": "en",
"text": "disorientate"
},
{
"language": "en",
"text": "take aback"
},
{
"language": "en",
"text": "set thinking"
}
],
"thesaurusLinks": [
{
"entry_id": "flummox",
"sense_id": "t_en_gb0005828.001"
}
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "verb",
"text": "Verb"
},
"text": "fox"
}
],
"type": "headword",
"word": "fox"
},
{
"id": "Fox",
"language": "en-gb",
"lexicalEntries": [
{
"entries": [
{
"inflections": [
{
"grammaticalFeatures": [
{
"id": "plural",
"text": "Plural",
"type": "Number"
}
],
"inflectedForm": "Fox"
}
],
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"a member of a North American people formerly living in southern Wisconsin, and now mainly in Iowa, Nebraska, and Kansas."
],
"domainClasses": [
{
"id": "people",
"text": "People"
},
{
"id": "amerindian",
"text": "Amerindian"
}
],
"id": "m_en_gbus0384960.006",
"semanticClasses": [
{
"id": "amerindian",
"text": "Amerindian"
}
],
"shortDefinitions": [
"member of North American people"
]
},
{
"definitions": [
"the Algonquian language of the Fox, now almost extinct."
],
"domainClasses": [
{
"id": "language",
"text": "Language"
},
{
"id": "amerindian",
"text": "Amerindian"
}
],
"id": "m_en_gbus0384960.010",
"notes": [
{
"text": "mass noun",
"type": "grammaticalNote"
}
],
"semanticClasses": [
{
"id": "amerindian_language",
"text": "Amerindian_Language"
}
],
"shortDefinitions": [
"Algonquian language of Fox, now extinct"
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "noun",
"text": "Noun"
},
"text": "Fox"
},
{
"entries": [
{
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"relating to the Fox or their language."
],
"domainClasses": [
{
"id": "amerindian",
"text": "Amerindian"
}
],
"id": "m_en_gbus0384960.013",
"shortDefinitions": [
"relating to Fox or their language"
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "adjective",
"text": "Adjective"
},
"text": "Fox"
}
],
"type": "headword",
"word": "Fox"
}
],
"word": "fox"
}
If there are any edits I should make to my question please let me know, this is my first question here. Thanks for any help.
EDIT:
The output needs to be formatted neatly, so basically it must be line separated and should be displayed in an easy to read manner, but as well as this, some elements, such as synonyms, need to have their words hyperlinked so they must be identified separately. Hope this helps
I have a json schema from which I need to generate java classes
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://schema.softsense.io/analysis/vulnerabilities/v1alpha1/vulnerabilities.json",
"title": "Vulnerabilities",
"description": "SoftSense Vulnerabilities analysis schema v1alpha3",
"type": "object",
"allOf": [ { "$ref": "https://schema.softsense.io/analysis/v1alpha3/base.json" } ],
"properties": {
"vulnerabilities": {
"items": {
"$ref": "#/definitions/Vulnerability"
},
"type": "array"
}
},
"oneOf": [{ "required": ["vulnerabilities"] }, { "required": ["error"] }],
"definitions": {
"Vulnerability": {
"required": [
"id",
"description",
"sources"
],
"properties": {
"id": {
"description": "Identifier for the vulnerability, such as a CVE identifier (CVE-2021-12345).",
"type": "string"
},
"published": {
"description": "UTC time when the vulnerability was published",
"type": "string",
"format": "date-time"
},
"updated": {
"description": "UTC time when the vulnerability was updated",
"type": "string",
"format": "date-time"
},
"description": {
"description": "Description of the vulnerability",
"type": "string"
},
"sources": {
"description": "The collection of backing vulnerability sources that listed this vulnerability for the software component",
"items": {
"type": "string"
},
"type": "array"
},
"cwes": {
"description": "The collection of Common Weakness Enumeration (CWE) identifiers that characterize the vulnerability.",
"items": {
"$ref": "#/definitions/CWE"
},
"type": "array"
},
"references": {
"description": "External URLs related to the vulnerability, such as issue trackers, mailing list discussions, commits, or patches.",
"items": {
"$ref": "#/definitions/Reference"
},
"type": "array"
},
"impact": {
"description": "What impact this vulnerability has",
"$ref": "#/definitions/Impact"
}
},
"type": "object"
},
"Impact": {
"properties": {
"metricV3": {
"$ref": "#/definitions/ImpactMetricV3"
},
"metricV2": {
"$ref": "#/definitions/ImpactMetricV2"
},
"*": {
"type": "object"
}
},
"type": "object"
},
"ImpactMetricV3": {
"properties": {
"cvssV3": {
"$ref": "#/definitions/CVSSv3"
},
"exploitabilityScore": {
"type": "number"
},
"impactScore": {
"type": "number"
}
}
},
"CVSSv3": {
"required": [
"version",
"vectorString",
"baseScore",
"baseSeverity"
],
"properties": {
"version": {
"type": "string"
},
"vectorString": {
"type": "string"
},
"attackVector": {
"type": "string"
},
"attackComplexity": {
"type": "string"
},
"privilegesRequired": {
"type": "string"
},
"userInteraction": {
"type": "string"
},
"scope": {
"type": "string"
},
"confidentialityImpact": {
"type": "string"
},
"integrityImpact": {
"type": "string"
},
"availabilityImpact": {
"type": "string"
},
"baseScore": {
"type": "number"
},
"baseSeverity": {
"type": "string"
},
"exploitCodeMaturity": {
"type": "string"
},
"remediationLevel": {
"type": "string"
},
"reportConfidence": {
"type": "string"
},
"temporalScore": {
"type": "number"
},
"temporalSeverity": {
"type": "string"
},
"confidentialityRequirement": {
"type": "string"
},
"integrityRequirement": {
"type": "string"
},
"availabilityRequirement": {
"type": "string"
},
"modifiedAttackVector": {
"type": "string"
},
"modifiedAttackComplexity": {
"type": "string"
},
"modifiedPrivilegesRequired": {
"type": "string"
},
"modifiedUserInteraction": {
"type": "string"
},
"modifiedScope": {
"type": "string"
},
"modifiedConfidentialityImpact": {
"type": "string"
},
"modifiedIntegrityImpact": {
"type": "string"
},
"modifiedAvailabilityImpact": {
"type": "string"
},
"environmentalScore": {
"type": "number"
},
"environmentalSeverity": {
"type": "string"
}
},
"type": "object"
},
"ImpactMetricV2": {
"required": [
"cvssV2",
"severity",
"exploitabilityScore",
"impactScore",
"obtainAllPrivilege",
"obtainUserPrivilege",
"obtainOtherPrivilege",
"userInteractionRequired"
],
"properties": {
"cvssV2": {
"$ref": "#/definitions/CVSSv2"
},
"severity": {
"type": "string"
},
"exploitabilityScore": {
"type": "number"
},
"impactScore": {
"type": "number"
},
"acInsufInfo": {
"type": "boolean"
},
"obtainAllPrivilege": {
"type": "boolean"
},
"obtainUserPrivilege": {
"type": "boolean"
},
"obtainOtherPrivilege": {
"type": "boolean"
},
"userInteractionRequired": {
"type": "boolean"
}
},
"type": "object"
},
"CVSSv2": {
"required": [
"version",
"vectorString",
"baseScore"
],
"properties": {
"version": {
"type": "string"
},
"vectorString": {
"type": "string"
},
"accessVector": {
"type": "string"
},
"accessComplexity": {
"type": "string"
},
"authentication": {
"type": "string"
},
"confidentialityImpact": {
"type": "string"
},
"integrityImpact": {
"type": "string"
},
"availabilityImpact": {
"type": "string"
},
"baseScore": {
"type": "number"
},
"exploitability": {
"type": "string"
},
"remediationLevel": {
"type": "string"
},
"reportConfidence": {
"type": "string"
},
"temporalScore": {
"type": "number"
},
"collateralDamagePotential": {
"type": "string"
},
"targetDistribution": {
"type": "string"
},
"confidentialityRequirement": {
"type": "string"
},
"integrityRequirement": {
"type": "string"
},
"availabilityRequirement": {
"type": "string"
},
"environmentalScore": {
"type": "number"
}
},
"type": "object"
},
"CWE": {
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
},
"type": "object"
},
"Reference": {
"required": [
"url"
],
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
},
"tags": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
}
}
I am trying to use the jsonschema2pojo as under.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.34</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.test.gen</targetPackage>
<useCommonsLang3>true</useCommonsLang3>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
For some reason I get the following error when i use mvn generate-sources
[ERROR] Failed to execute goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate (default) on project hsk: Execution default of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: String index out of range: 0 -> [Help 1]
Any help is highly appreciated
Regards,
I am using the Maven Avro plugin to generate POJOs from an AVSC file. Unfortunately, the top-most class that is generated from this file - loanInitiate - does not appear to contain any of the getters and setters needed for the other structures within the schema.
The POJOs that are generated for the child nodes do have the expected getters and setters. For example, the Customer class is generated and has a getNameBase() method.
I have tried editing this file in a variety of ways - changing structure, etc. - but I keep running into situations where the format is not valid.
Why would some of the getters and setters be generated and not others?
The Avro schema file is defined as follows:
{
"fields": [
{
"name": "loanInitiate",
"type": [
{
"type": "record",
"name": "eventheader",
"fields": [
{
"name": "eventType",
"type": "string",
"doc": "Loan"
},
{
"name": "eventSubtype",
"type": "string",
"doc": "Initiate"
},
{
"name": "eventDateTime",
"type": "string"
},
{
"name": "eventGeneratedDateTime",
"type": "string"
},
{
"name": "eventCorrelationId",
"type": [
"string",
"null"
]
},
{
"name": "eventRequestId",
"type": [
"string",
"null"
]
},
{
"name": "eventSourceDescription",
"type": [
"string",
"null"
]
},
{
"name": "eventSource",
"type": [
"string",
"null"
],
"doc": "The producer of this message."
},
{
"name": "eventInitiator",
"type": [
"string",
"null"
]
},
{
"name": "eventBatchGroupId",
"type": [
"string",
"null"
],
"doc": "BatchGroupID represents the GUID value given to each individual message which is part of a single batch."
},
{
"name": "eventBatchRecordCountTotal",
"type": [
"string",
"null"
],
"doc": "BatchRecordCountTotal provides the total number of messages belonging to a single batch."
}
]
},
{
"type": "array",
"name": "customer",
"items": {
"name": "customer",
"type": "record",
"fields": [
{
"name": "nameBase",
"type": {
"type": "record",
"name": "nameBase",
"fields": [
{
"name": "givenName",
"type": "string"
},
{
"name": "otherGivenName",
"type": [
"string",
"null"
]
},
{
"name": "fullName",
"type": "string"
},
{
"name": "suffix",
"type": [
"string",
"null"
]
},
{
"name": "surname",
"type": "string"
},
{
"name": "titlePrefix",
"type": [
"string",
"null"
]
}
]
}
},
{
"name": "party",
"type": {
"type": "record",
"name": "party",
"fields": [
{
"name": "parentEntityId",
"type": "string",
"doc": "The value of this field should equal the value the id field of the parent object."
},
{
"name": "partyRole",
"type": {
"type": "record",
"name": "partyRole",
"fields": [
{
"name": "code",
"type": {
"name": "code",
"type": "enum",
"symbols": [
"INSD",
"OWNR",
"PPAY",
"RCP"
]
}
},
{
"name": "subCode",
"type": {
"name": "subCode",
"type": "enum",
"symbols": [
"PRMR",
"LIST",
"BEN",
"DRBR",
"JNT"
]
}
}
]
}
}
]
}
},
{
"name": "id",
"type": "string",
"doc": "This is the identifier for an instance of customer, which can be related to from other objects."
}
]
}
},
{
"name": "bankAccount",
"type": "record",
"doc": "This object represents the bank account information, required for loan disbursements via ACH.",
"fields": [
{
"name": "accountType",
"type": {
"name": "accountType",
"type": "enum",
"symbols": [
"CHKNG",
"SVNG"
]
}
},
{
"name": "fullName",
"type": "string"
},
{
"name": "bankName",
"type": "string"
},
{
"name": "accountNumber",
"type": "string"
},
{
"name": "routingNumber",
"type": "string"
}
]
},
{
"type": "record",
"name": "trackingeventdetails",
"fields": [
{
"name": "workEventIdent",
"type": [
"string",
"null"
],
"doc": "This field will be populated based on the creation of a tracking work event. This field value holds the key for that particular event."
},
{
"name": "applicationId",
"type": "string"
},
{
"name": "divisionCode",
"type": "string"
},
{
"name": "departmentCode",
"type": "string"
},
{
"name": "workEventNumber",
"type": "int"
},
{
"name": "longComment",
"type": "string"
},
{
"name": "actualEventDate",
"type": "string"
},
{
"name": "priorityCode",
"type": "string"
},
{
"name": "contactTypeCode",
"type": "string"
},
{
"name": "receivedDate",
"type": "string"
},
{
"name": "shortComment",
"type": "string"
},
{
"name": "lastUpdatedBy",
"type": "string"
},
{
"name": "loggedByIdent",
"type": "string"
},
{
"name": "resourceId",
"type": "string"
},
{
"name": "requestOrTypeCode",
"type": "string"
},
{
"name": "serviceChannelSourceCode",
"type": "string"
},
{
"name": "nigoCode",
"type": "string"
},
{
"name": "imageAvailableIndicator",
"type": "string"
},
{
"name": "completionIndicator",
"type": "string"
}
]
},
{
"type": "record",
"name": "policy",
"fields": [
{
"name": "id",
"type": "int"
},
{
"name": "number",
"type": "long"
},
{
"name": "agreementNumberPrefix",
"type": "string"
},
{
"name": "agreementNumberSuffix",
"type": "string"
},
{
"name": "adminSystem",
"type": "string"
}
]
},
{
"type": "record",
"name": "loantransactiondetails",
"doc": "This represents the values specific to the loan transaction.",
"fields": [
{
"name": "transactionAmount",
"type": "string",
"doc": "The amount of the loan."
},
{
"name": "transactionType",
"type": "string"
},
{
"name": "transferAmount",
"type": {
"name": "transferAmount",
"type": "enum",
"symbols": [
"Max",
"Other"
]
}
}
]
}
]
}
],
"type": "record",
"namespace": "com.stream.process",
"name": "loanInitiate"
}
i have Json string like this where the function return List:
[
{
"name": "DEFAULT",
"effectiveDate": "Apr 19, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 20, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "NO_BILLING_PERIOD",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 21, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 21, 2018 3:38:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
}
]
where json build by this code:
String json = new Gson().toJson(price);
then i want to get single value, for example :
"plans":["postpaid-monthly-fix","postpaid-monthly-trial1","postpaid-monthly-trial7","prepaid-signature-item"]
i try with json.get("plans") but get syntax erro, any clue ?
Try the following:
String s = "[{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 19, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 20, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"NO_BILLING_PERIOD\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 21, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 21, 2018 3:38:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]}]\n";
JSONArray jsonArray = (JSONArray) JSONValue.parseWithException(s);
List plans = (List)((Map)((List)((Map)jsonArray.get(0)).get("priceLists")).get(0)).get("plans");
System.out.println(plans);
System.out.println(plans.get(0));
System.out.println(plans.get(1));
System.out.println(plans.get(2));
System.out.println(plans.get(3));
Output:
["postpaid-monthly-fix","postpaid-monthly-trial1","postpaid-monthly-trial7","prepaid-signature-item"]
postpaid-monthly-fix
postpaid-monthly-trial1
postpaid-monthly-trial7
prepaid-signature-item
Below is my example search response with 4 results retrieved.
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.41753215,
"hits": [
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/shared/Report_google_Shared",
"_score": 0.41753215,
"fields": {
"lastModified": [
1461738428007
],
"dir": [
"/shared"
],
"filename": [
"Report_google_Shared"
]
},
"highlight": {
"filename": [
"Report_google_Shared"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/shared/Report_Gmail_Shared",
"_score": 0.41753215,
"fields": {
"lastModified": [
1461738618676
],
"dir": [
"/shared"
],
"filename": [
"Report_Gmail_Shared"
]
},
"highlight": {
"filename": [
"Report_Gmail_Shared"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/private/hitesh/Report_Gmail_Private",
"_score": 0.1883173,
"fields": {
"lastModified": [
1461738629888
],
"dir": [
"/private/hitesh"
],
"filename": [
"Report_Gmail_Private"
]
},
"highlight": {
"filename": [
"Report_Gmail_Private"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/private/dholaria/Report_google_Private",
"_score": 0.1883173,
"fields": {
"lastModified": [
1461738451720
],
"dir": [
"/private/dholaria"
],
"filename": [
"Report_google_Private"
]
},
"highlight": {
"filename": [
"Report_google_Private"
]
}
}
]
}
}
Now, I want to filter the above search results basis the specific "dir" field value as per the below criteria.
Include the search result in the response if and only if:
If "dir" field value equals to either of: "/shared" or "/private/hitesh"
Else if "dir" field value starts with either of: "/shared/" or
"/private/hitesh/"
How can I achieve the above functionality in ElasticSearch?
PS: Below is my example mapping.
{
"google_a804f89b-d32e-426a-a79a-ea83d65c98ea": {
"mappings": {
"viz_dashlet": {
"properties": {
"charts": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"columnLabels": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"columnNames": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"creator": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"dimensions": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"dir": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"expressions": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"filename": {
"type": "string",
"index_analyzer": "whitespace_index",
"search_analyzer": "whitespace_search",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"lastModified": {
"type": "date",
"format": "date_hour_minute_second"
},
"measures": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"promptFilters": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
Try this query:
{
"query": {
"bool": {
"should": [
{
"term": {
"dir.raw": {
"value": "/shared"
}
}
},
{
"term": {
"dir.raw": {
"value": "/private/hitesh"
}
}
},
{
"match_phrase_prefix": {
"dir.raw": "/shared"
}
},
{
"match_phrase_prefix": {
"dir.raw": "/private/hitesh"
}
}
]
}
}
}