Json query String for getting value with Condition - java

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")

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.

Regex to replace key & value in JSON

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

MongoDB query and select inner object

I want to query for inner object and select only filtered inner objects from mongoddb document.
Consider below mongodb document.
{
"schools": [
{
"name": "ABC",
"students": [
{
"name": "ABC 1",
"class": 1
},
{
"name": "ABC 2",
"class": 2
},
{
"name": "ABC 3",
"class": 1
}
]
},
{
"name": "XYZ",
"students": [
{
"name": "XYZ 1",
"class": 1
},
{
"name": "XYZ 2",
"class": 2
}
]
}
]
}
I want to select only students in class 1.
expected result json as below.
{
"school": {
"name": "ABC",
"students": [
{
"name": "ABC 1",
"class": 1
},
{
"name": "ABC 3",
"class": 1
}
]
},
"school": {
"name": "XYZ",
"students": [
{
"name": "XYZ 1",
"class": 1
}
]
}
}
Even below result is fine with me.
{
"students": [
{
"name": "ABC 1",
"class": 1
},
{
"name": "ABC 3",
"class": 1
},
{
"name": "XYZ 1",
"class": 1
}
]
}
Please help me to get this done.
Really helpful if can provide mongodb query.
I am using mongodb with spring data in my application.
You can search for mongo db array nested record search. The example code is here. Document is here.
db.your_collection_name.find({'school.student.class':1})
If you only want students do flatmap for your results. Here is document for flatmap in mongodb
Finally I could be able to find the query.
First I have to unwind and apply matching criteria. this was work for me.
db.{mycollection}.aggregate(
[
{ $unwind: '$schools.students'},
{ $match : { "schools.students.class" : 1 } },
{ $project : { "schools.name" : 1, 'schools.students' : 1 } }
]
);

Having trouble accessing fields of JSON using groovy

I'm trying to extract the information from an API response using groovy
I want to get to the info stored under the "res" key.
Something along the lines of body.measures.02:00:00:02:06:70.res
Can not figure out have to access this information without throwing a nullpointerexception.
"body":[ {
"_id":"70:ee:50:01:fe:96",
"place":{
"location":[
-70.863189,
42.273936
],
"altitude":26.154942,
"timezone":"America\/New_York"
},
"mark":12,
"measures":{
"02:00:00:02:06:70":{
"res":{
"1506611038":[
22,
66
]
},
"type":[
"temperature",
"humidity"
]
},
"06:00:00:01:97:28":{
"wind_strength":15,
"wind_angle":343,
"gust_strength":29,
"gust_angle":301,
"wind_timeutc":1506611083
},
"70:ee:50:01:fe:96":{
"res":{
"1506611086":[
1007.4
]
},
"type":[
"pressure"
]
}
},
"modules":[
"02:00:00:02:06:70",
"06:00:00:01:97:28"
],
"module_types":{
"02:00:00:02:06:70":"NAModule1",
"06:00:00:01:97:28":"NAModule2"
}
}
],
"status":"ok",
"time_exec":0.017483949661255,
"time_server":1506611446
}
body is an array with one element in your case, so you have to do body[0].measures.'02:00:00:02:06:70'.res, e. g. like
new groovy.json.JsonSlurper().parseText('''{"body":[ {
"_id":"70:ee:50:01:fe:96",
"place":{
"location":[
-70.863189,
42.273936
],
"altitude":26.154942,
"timezone":"America/New_York"
},
"mark":12,
"measures":{
"02:00:00:02:06:70":{
"res":{
"1506611038":[
22,
66
]
},
"type":[
"temperature",
"humidity"
]
},
"06:00:00:01:97:28":{
"wind_strength":15,
"wind_angle":343,
"gust_strength":29,
"gust_angle":301,
"wind_timeutc":1506611083
},
"70:ee:50:01:fe:96":{
"res":{
"1506611086":[
1007.4
]
},
"type":[
"pressure"
]
}
},
"modules":[
"02:00:00:02:06:70",
"06:00:00:01:97:28"
],
"module_types":{
"02:00:00:02:06:70":"NAModule1",
"06:00:00:01:97:28":"NAModule2"
}
}
],
"status":"ok",
"time_exec":0.017483949661255,
"time_server":1506611446
}}''').body[0].measures.'02:00:00:02:06:70'.res

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"
},
{
...
}
]
}

Categories