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
Related
I want get only age value if age is greater than 0 (age > 0). Please help the json path expression
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
}
}
I have tried like this "$.[?($.age > 0)]" but not working
private void ageCondiionCheck(ReadContext context) {
Gson gson = new GsonBuilder().serializeNulls().create();
String jsonString = gson.toJson(customerJSON);
Configuration conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL;
ReadContext context = JsonPath.using(conf).parse(jsonString);
String jsonPath = "$.[?($.age > 0)]"
Object result = context.read(jsonPath);
System.out.println("Age Value greater than zero : "+result.toString());
}
I assume that your input JSON string is supposed to be a JSON array as follows: (I ignored some fileds)
[
{
...,
"age": 26,
...
},
{
...,
"age": 0,
...
},
{
...,
"age": 18,
...
}
]
Code snippet
It can be easily achieved by using Jayway JsonPath.
System.out.println(JsonPath.parse(jsonStr).read("$[?(#.age > 0)].age").toString());
Console output
[26,18]
one possible way (ES6) ... consider you have an array of objects as below
var data = [
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
}
},
{
"firstName": "Jimmy",
"lastName" : "Bimbo",
"age" : 0,
"address" : {
"streetAddress": "Lime street",
"city" : "NYC",
"postalCode" : "100-0002"
}
}
]
var filtered = data.filter(item => item.age > 0)
console.log(filtered)
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/
I am trying to flatten nested arrays using aggregation framework but I can not get the result I which.
my collection is :
[
{
"id" : "xxx",
"countryName" : "xxx",
"cities" : [
{
"id" : "xxx",
"cityName" : "xxx"
},
{
"id" : "xxx",
"cityName" : "xxx"
}
]
}
]
I want to get the cities from all countries, the result I am looking for is :
[
{
"id" : "xxx",
"cityName" : "xxx"
},
{
"id" : "xxx",
"cityName" : "xxx"
}
]
I tried this request :
val aggregation = Aggregation.newAggregation(
Aggregation.group("cities")
)
return mongoDb.aggregate(aggregation, Country::class.java, Any::class.java).mappedResults
But, I got this result :
[
{
"_id": [
{
"id": "xxx",
"cityName": "xxx"
},
{
"id": "xxx",
"cityName": "xxx"
}
]
}
]
Can someone help me please?
This aggregation will help you achieve your result, except that you have to adapt it with Java driver:
db.countries.aggregate([
{
"$unwind": "$cities"
},
{
"$project": {
"_id": 0,
"cities": 1
}
},
{
"$replaceRoot": {
"newRoot": "$cities"
}
}
])
I am trying to build an AVRO's complex record with Union data type supported member record type.
{
"namespace": "proj.avro",
"protocol": "app_messages",
"doc" : "application messages",
"types": [
{
"name": "record_request",
"type" : "record",
"fields":
[
{
"name" : "request_id",
"type" : "int"
},
{
"name" : "message_type",
"type" : int,
},
{
"name" : "users",
"type" : "string"
}
]
},
{
"name" : "request_response",
"type" : "record",
"fields" :
[
{
"name" : "request_id",
"type" : "int"
},
{
"name" : "response_code",
"type" : "string"
},
{
"name" : "response_count",
"type" : "int"
},
{
"name" : "reason_code",
"type" : "string"
}
]
}
]
"messages" :
{
"published_msgs" :
{
"doc" : "My Messages",
"fields" :
[
{
"name" : "message_type",
"type" : "int"
},
{
"name" : "message_body",
"type" :
[
"record_request", "request_response"
]
}
]
}
}
}
I am getting error while trying to read this kind of schema.
I would like to know - is it possible to declare such AVRO schema - which has one of field which type is union of complex user defined message structure.
If its possible then could you please let me know what i am doing wrong or an example of such structure with union type field's type definition?
I want to use AVRO's dynamically schema usage - so specify this schema file run-time and parse the incoming buffer as "request"/"response".
Thanks,
It is possible define an union of complex types, the problem with your schema is that it is not defined at field level. Your schema must looks like this to achieve the union of complex types
{
"namespace": "proj.avro",
"protocol": "app_messages",
"doc" : "application messages",
"name": "myRecord",
"type" : "record",
"fields": [
{
"name": "requestResponse",
"type": [
{
"name": "record_request",
"type" : "record",
"fields":
[
{
"name" : "request_id",
"type" : "int"
},
{
"name" : "message_type",
"type" : "int"
},
{
"name" : "users",
"type" : "string"
}
]
},
{
"name" : "request_response",
"type" : "record",
"fields" :
[
{
"name" : "request_id",
"type" : "int"
},
{
"name" : "response_code",
"type" : "string"
},
{
"name" : "response_count",
"type" : "int"
},
{
"name" : "reason_code",
"type" : "string"
}
]
}
]
}
]
}
I have gone through various threads, but couldn't find the particular answer in python.
I have a json file
{
"StoreID" : "123",
"Status" : 3,
"data" : {
"Response" : {
"section" : "25",
"elapsed" : 277.141,
"products" : {
"prd_1": {
"price" : 11.99,
"qty" : 10,
"upc" : "0787493"
},
"prd_2": {
"price" : 9.99,
"qty" : 2,
"upc" : "0763776"
},
"prd_3": {
"price" : 29.99,
"qty" : 8,
"upc" : "9948755"
}
},
"type" : "Tagged"
}
}
}
I need to convert this json file into the format below, by changing json object 'products' into an array form.
{
"StoreID" : "123",
"Status" : 3,
"data" : {
"Response" : {
"section" : "25",
"elapsed" : 277.141,
"products" : [
{
"price" : 11.99,
"qty" : 10,
"upc" : "0787493"
},
{
"price" : 9.99,
"qty" : 2,
"upc" : "0763776"
},
{
"price" : 29.99,
"qty" : 8,
"upc" : "9948755"
}
],
"type" : "Tagged"
}
}
}
Is there any good way to do it in python. Mostly I saw people are using java, but not in python. Can you please let me know a way to do it in python.
Just get the values() of products dictionary and that will give you an array of values. Code below works from me assuming your json is in file1.txt Also note
import json
with open('file1.txt') as jdata:
data = json.load(jdata)
d = data
d["data"]["Response"]["products"] = d["data"]["Response"]["products"].values()
print(json.dumps(d))
output:
{"Status": 3, "StoreID": "123", "data": {"type": "Tagged", "Response": {"section": "25", "products": [{"price": 9.99, "upc": "0763776", "qty": 2}, {"price": 29.99, "upc": "9948755", "qty": 8}, {"price": 11.99, "upc": "0787493", "qty": 10}], "elapsed": "277.141"}}}
Would something like this work for you?
import json
import copy
a = json.load(open("your_data.json", "r"))
b = copy.deepcopy(a)
t = a.get('data').get('Response').get('products')
b['data']['Response']['products'] = t.values() # Originally was: [t[i] for i in t]
You can give back JSON with json.dumps(b)