Regex to replace key & value in JSON - java

I'm trying to remove the below key and valuefrom the JSON string in Java. I couldn't really crack the pattern. Can anyone help me find what I'm doing wrong here?
"appointment_request_id": "77bl5ii169daj0abqaowl0ggmnwxdk1219mug023", // (, included)
String newTransformedJsonString = jsonString.replaceAll("\"appointment_request_id\":\".*\",","");
I think I need to add a wildcard to make sure the starting and ending " for value are considered. I tried ?, surrounding the " as ["]. No luck.
The value will never be empty.
The value will have spaces trimmed
Assume the value can have any character
{
"appointment_request_id": "77bl5ii169daj0abqaowl0ggmnwxdk1219mug023",
"app_spec_version": "0.0.61-5",
"previous_invoice_ids": [
"18000-A-qa4wl0kvka",
"18101-A-y49daj0ppp"
],
"contracts": [
{
"name": "bcbs.patient",
"definitions": [
{
"base_path": "/patient/v1",
"swagger": {
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "patient-v1"
},
"basePath": "",
"tags": [
{
"name": "patient-v1",
"description": "PatientServiceResource"
}
],
"schemes": [
"http"
],
"webpages": {
"/patient/v1/insurace": {
"get": {
"tags": [
"patient-v1"
],
"summary": "Returnsanerror,butwaitsbeforedoingso.",
"operationId": "getInsurance",
"produces": [
"application/json"
],
"parameters": [
{
"name": "statusCode",
"in": "query",
"description": "",
"required": false,
"type": "integer",
"format": "int32"
}
]
}
}
}
}
}
]
}
]
}

This may be helpful.
How to make a regex to replace the value of a key in a json file

Related

How to get json key values by another key value

I have a JSON output like this:
{
"items": [
{
"id": "1",
"name": "Anna",
"values": [
{
"code": "Latin",
"grade": 1
},
{
"code": "Maths",
"grade": 5
}
]
},
{
"id": "2",
"name": "Mark",
"values": [
{
"code": "Latin",
"grade": 5
},
{
"code": "Maths",
"grade": 5
}
]
}
]
}
I need to get field values for "name": "Anna". I am getting RestAssured Response and would like to use my beans to do that, but I can also use jsonPath() or jsonObject(), but I don't know how. I searched many topics but did not find anything.

Jackson/Gson get specified key from Json - Java

i have problem with get specified value from json. I need get Key value from json below, its start with array/list.
[
{
"Version": 1,
"Key": "353333_PC",
"Type": "PostalCode",
"Rank": 500,
"LocalizedName": "Suwalki",
"EnglishName": "Suwalki",
"PrimaryPostalCode": "16-400",
"Region": {
"ID": "EUR",
"LocalizedName": "Europe",
"EnglishName": "Europe"
},
"Country": {
"ID": "PL",
"LocalizedName": "Poland",
"EnglishName": "Poland"
}
}
]
I have tried code like this:
String content = parent.path("Key").asText();
return content;
but it returns empty string. Have u any idea how get this?

Json query String for getting value with Condition

I want to get the value of vehicleCodeelement where vehicleLease is AS PER LAW OF ENFORCMENT.
Could someone please help me with JSON query ?
Please find JSON data below.
{
"StateOffers": [
[{
"vehicleCode": 242214214,
"vehiclePart": [{
"#type": "vehiclePart",
"segments": [{
"#type": "Segment"
"segmentOfferInformation": {
"vehicleMiles": 771,
"awardFare": false
},
"vehicleLease": "AS PER LAW OF ENFORCMENT"
}
],
"stops": 0
}
]
}, {
"vehicleCode": 242214214,
"vehiclePart": [{
"#type": "vehiclePart",
"segments": [{
"#type": "Segment"
"segmentOfferInformation": {
"vehicleMiles": 771,
"awardFare": false
},
"vehicleLease": "MY RULE MY WORLD"
}
],
"stops": 0
}
]
}
]
]
}
This is the expression I have tried:
$..vehicleCode[?($..vehicleLease=="AS PER LAW OF ENFORCMENT")

How to index a Json object with object and its reference in elasticsearch?

I am working with Elasticsearch recently, and I meet a problem that don't know how to solve it.
I have a Json like:
{
"objects": [
"object1": {
"id" : "12345",
"name":"abc"
},
"12345"
]
}
Object2 is a reference of object1, when I trying to saving(or called indexing) into elastic search, it says:
"org.elasticsearch.index.mapper.MapperParsingException: failed to parse"
After I google I found that because object1 is an object, but object 2 is considered as a string.
We cannot change our json in our project, so in this case how can I save it in the elasticsearch?
Thanks for any help and suggestion.
How do you do that?
I run this command and it works.
PUT test/t1/1
{
"objects": {
"object1": {
"id" : "12345",
"name":"abc"
},
"object2": "12345"
}
}
and the result is:
{
"_index": "test",
"_type": "t1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
UPDATE 1
Depending on your requirements one of these may solve your problem:
PUT test/t1/2
{
"objects": [
{
"object1": {
"id": "12345",
"name": "abc"
}
},
{
"object2": "12345"
}
]
}
PUT test/t1/2
{
"objects": [
{
"object1": {
"id": "12345",
"name": "abc"
},
"object2": "12345"
},
{
...
}
]
}

How to count by attribute in JSON?

I have the following JSON:
{
"items": [
{
"id": "1",
"name": "John",
"location": {
"town": {
"id": "10"
},
"address": "600 Fake Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "2",
"name": "Sarah",
"location": {
"town": {
"id": "10"
},
"address": "76 Evergreen Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "3",
"name": "Hamed",
"location": {
"town": {
"id": "20"
},
"address": "50 East A Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
}
]
}
And I need to get something like this, count how many times each townId appears:
[ { "10": 2 }, {"20": 1 }]
I'm trying to find the most eficient way to do this. Any idea?
Most efficient way is to load the String in a StringBuilder and remove all line breaks and white spaces. Then search for index of "town":{"id":" string (town start index) and then search for the end index (String `"}'). Using the 2 indexes you can extract town ids and count them.
No need to deserialize the JSON into POJO objects:) and extract values by xpath from the POJOs.

Categories