I have below JSON data. I have JPath to read FirstName and LastName but to read both values using a single JSONPath expression is not getting. Can someone help me here to read the values for both the elements?
I want to read like Name=Rob|Long using JSONPath expression. I tried a few combinations but not working
{
"attributes": {
"type": "Contacts",
"url": "/services/data/v36.0/sobjects/Contact/abc123"
},
"Id": "abc123",
"Salutation": "Mr.",
"FirstName": "Rob",
"LastName": "Long"
}
Thanks in advance
Try with JayWay JsonPath:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
This should do the trick: concat($.FirstName,"|",$.LastName)
However, it doesn't seem to work with other JsonPath implementations.
using Bazaarvoice/ jolt this can be easily achieved
--------JSON Input
{
"attributes": {
"type": "Contacts",
"url": "/services/data/v36.0/sobjects/Contact/abc123"
},
"Id": "abc123",
"Salutation": "Mr.",
"FirstName": "Rob",
"LastName": "Long"
}
--------------spec
[
{
"operation": "modify-default-beta",
"spec": {
// String join the values in the array x, with a comma and a space
"fullName": "=concat(#(1,FirstName),'|',#(1,LastName))"
}
}
]
--------------------output
{
"attributes" : {
"type" : "Contacts",
"url" : "/services/data/v36.0/sobjects/Contact/abc123"
},
"Id" : "abc123",
"Salutation" : "Mr.",
"FirstName" : "Rob",
"LastName" : "Long",
"fullName" : "Rob|Long"
}
try this out at - https://jolt-demo.appspot.com/
Related
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
Dear MongoDb Experts out there! I am new to Mongo and I am currently working with a MongoDb with Java.
Having a MongoDb with a collection called "teams" with the following example structure:
{[
{
"id": "123",
"name": "Dev1",
"employees": [
{"name": "John", "age": 30},
{"name": "Jane", "age": 30}
]
},
{
"id": "456",
"name": "Dev2",
"employees": [
{"name": "Mike", "age": 30},
{"name": "Oscar", "age": 27}
]
}
]}
I want to have a query which returns an array with all employees, that are 30 years old. So the expected result would be:
{[
{"name": "John", "age": 30},
{"name": "Jane", "age": 30},
{"name": "Mike", "age": 30}
]}
It would be even better to only get the employees name (since I know the age I searched for), like:
{[
{"name": "John"},
{"name": "Jane"},
{"name": "Mike"}
]}
I have a MongoCollection object:
MongoCollection<Document> collection = mongoClient
.getDatabase(databaseName)
.getCollection("teams")
My question: Is it possible to retrieve my expected result from the MongoDb? If so, which operations do I have to call on my collection object?
Approach 1: Find with $elemMatch projection
db.getCollection('teams').find(
{"employees.age":30},
{ "employees": { "$elemMatch": { "age": 30 } },"_id":0 }
)
Output Format:
{
"employees" : [
{
"name" : "John",
"age" : 30.0
}
]
}
{
"employees" : [
{
"name" : "Mike",
"age" : 30.0
}
]
}
Approach 2: Aggregate with $unwind and key renaming using $projection
db.getCollection('teams').aggregate([
{"$match": {"employees.age":30}} ,
{"$unwind": "$employees"},
{"$match": {"employees.age":30}} ,
{"$project": {"name": "$employees.name","_id":0}}
])
Output format:
{
"name" : "John"
}
{
"name" : "Jane"
}
{
"name" : "Mike"
}
Approach 1 would be faster, but require additional work at app layer. It is recommended to offload formatting tasks to app servers rather than on db itself.
Approach 2 can instantly give to the formatted results, doing the querying and formatting both in the database servers
I am trying to do Transform the jolt but I face problems in the nested array. I am not getting the description value.
I put some code
JSON Input is
{
"id": 3,
"name": "Sample Product",
"attribute_set_id": 4,
"price" : 10,
"custom_attributes": [
{
"attribute_code": "image",
"value": "/1/_/1.jpg"
},
{
"attribute_code": "description",
"value": "<p>This is Sample Product for test</p>"
}
]
}
And my Jolt Spec is
[
{
"operation": "shift",
"spec": {
"id": "id",
"name": "name",
"description": "custom_attributes[0].attribute_code.value"
}
]
My expected output is
{
"id" : 3,
"name" : "Sample Product",
"description" : "<p>This is Sample Product for test</p>",
"image" : "/1/_/1.jpg",
"start_price" : 10,
"current_price" : 10
}
If "attribute_code": "image" and "attribute_code": "description" always appears in the 1st and 2nd element of the array, you can simply transform the given JSON string with following Jolt Spec:
[
{
"operation": "shift",
"spec": {
"id": "id",
"name": "name",
"custom_attributes": {
"1": {
"value": "description"
},
"0": {
"value": "image"
}
},
"price": ["start_price", "current_price"]
}
}
]
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"
},
{
...
}
]
}
I was trying to find a way to convert the json array to json string.
http://jsonpath.com/
JSON
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : ["iPhone"],
"number": "0123-4567-8888"
},
{
"type" : ["home"],
"number": "0123-4567-8910"
}
]
}
Output
iphone
Expression I tried,
$.phoneNumbers[:1].type[,]
$.phoneNumbers[:1].type
$.phoneNumbers[:1].type
Thanks in advance