how to change json request to payload form - java

I have a json request like this:
{
"record_type": "item",
"data": [
{
"item_id_wms": 75985,
"item_type": "Inventory",
"item_code": "ITEM8080808",
"display_name": "ITEM E888",
"category_item": "",
"upc_code" : 12345678,
"base_unit": "BOX",
"height": 12,
"width": 13,
"depth": 12,
"uom_volume": 10
}
]
}
but I hit use request.setPayload(request.toString()); I got error like this when hitting my other service
Response Body : {"error" : {"code" : "SYNTAX_ERROR", "message" : "org.mozilla.javascript.EcmaError: SyntaxError: Unexpected token: c (INVOCATION_WRAPPER$sys#24)."}}
but if I manually use this code: I can hit my endpoint.
OAuthRequest requestSendToNS = new OAuthRequest(Verb.POST, baseUrl);
requestSendToNS.setPayload(("{\"record_type\":\"item\", \"data\": [ { \"item_id_wms\": 99920, \"item_type\": \"kit\", \"item_code\": \"ITEM808080\", \"display_name\": \"ITEM E888\", \"category_item\": \"kit\", \"upc_code\" : 12345678, \"base_unit\": \"PCS\", \"height\": 12, \"width\": 13, \"depth\": 12, \"uom_volume\": 10, \"component\" : [ { \"item_id_component\":\"ITMIDAS0000000007\", \"quantity_component\": 18, \"unit_id_component\":\"PCS\" }, { \"item_id_component\":\"ITMIDAS0000000001\", \"quantity_component\": 15, \"unit_id_component\":\"PCS\" } ] }, { \"item_id_wms\": 99922, \"item_type\": \"kit\", \"item_code\": \"ITEM808080\", \"display_name\": \"ITEM E888\", \"category_item\": \"kit\", \"upc_code\" : 12345678, \"base_unit\": \"PCS\", \"height\": 12, \"width\": 13, \"depth\": 12, \"uom_volume\": 10, \"component\" : [ { \"item_id_component\":\"ITMIDAS0000000007\", \"quantity_component\": 18, \"unit_id_component\":\"PCS\" }, { \"item_id_component\":\"ITMIDAS0000000001\", \"quantity_component\": 15, \"unit_id_component\":\"PCS\" } ] }, { \"item_id_wms\": 99924, \"item_type\": \"kit\", \"item_code\": \"ITEM808080\", \"display_name\": \"ITEM E888\", \"category_item\": \"kit\", \"upc_code\" : 12345678, \"base_unit\": \"PCS\", \"height\": 12, \"width\": 13, \"depth\": 12, \"uom_volume\": 10, \"component\" : [ { \"item_id_component\":\"ITMIDAS0000000007\", \"quantity_component\": 18, \"unit_id_component\":\"PCS\" }, { \"item_id_component\":\"ITMIDAS0000000001\", \"quantity_component\": 15, \"unit_id_component\":\"PCS\" } ] } ] }"));
How can I change my json request come in the payload format like that?

Related

Java Akka http post method Unable to unmarshall nested request json

I am trying to send Nested json in Post request body to call akkhttp route, but getting exception,
Cannot unmarshall JSON as Campaign
Someone Please help me out to resolve the issue.
below is code
concat(
post(() -> pathPrefix(PathMatchers.segment(ACCOUNTS_SEGMENT).slash(PathMatchers.segment()), (accountId) ->
path(PathMatchers.segment(JOBS_SEGMENT).slash(PathMatchers.uuidSegment()) , (jobId) -> {
**return entity(Jackson.unmarshaller(Campaign.class), campaign -> {**
System.out.println("###############"+campaign.getName());
CompletionStage<Done> futureSaved = executeCampaignProcess(jobId.toString(), accountId);
return onSuccess(futureSaved, done ->
complete(StatusCodes.ACCEPTED, ACCEPTED_EXECUTE_CAMPAIGN_REQUEST)
);
});
}
)))
Nested json in a request body is as below
{
"id" : "2d2cee47-40c9-4ebe-80bb-f8a38e6379f9",
"name" : "DDDAmol",
"description" : "",
"type" : "FINITE",
"senderDisplayName" : "",
"senderAddress" : "",
"dialingOrder" : [ "PRIORITY", "RETRY", "REGULAR" ],
"finishType" : "FINISH_AFTER",
"finishTime" : null,
"finishAfter" : 0,
"checkTimeBasedFinishCriteria" : false,
"createdOn" : [ 2023, 1, 31, 8, 12, 40, 32309000 ],
"updatedOn" : [ 2023, 2, 14, 14, 11, 4, 758967300 ],
"lastExecutedOn" : [ 2023, 2, 18, 13, 51, 28, 821000000 ],
"contactList" : "CD_06_SMS_QUEUED_DELAYED",
"rule" : null,
"strategy" : {
"id" : "a41d8895-7a67-4cce-a39b-5a6ee5e8b4a9",
"name" : "Simple",
"type" : "SMS",
"description" : "",
"smsText" : "Hello",
"smsPace" : 40,
"smsPaceTimeUnit" : "SECOND",
"createdOn" : [ 2023, 1, 31, 8, 12, 15, 209992700 ],
"updatedOn" : [ 2023, 1, 31, 8, 12, 15, 209992700 ]
}
}
Refering below link but no working :(
https://doc.akka.io/docs/akka-http/current/common/json-support.html

Deserialize(convert) a JsonObject to DateTime without POJO

I have the following jsonObject:
"dateCreated": {
"year": 2021,
"dayOfYear": 145,
"equalNow": false,
"weekyear": 2021,
"chronology": {
"zone": {
"ID": "UTC"
}
},
"weekOfWeekyear": 21,
"secondOfMinute": 10,
"millisOfDay": 10990000,
"monthOfYear": 5,
"dayOfWeek": 2,
"beforeNow": true,
"minuteOfDay": 183,
"dayOfMonth": 25,
"era": 1,
"zone": {
"ID": "UTC"
},
"yearOfCentury": 21,
"centuryOfEra": 20,
"hourOfDay": 3,
"secondOfDay": 10990,
"millis": 1621911790000,
"yearOfEra": 2021,
"minuteOfHour": 3,
"millisOfSecond": 0,
"afterNow": false
}
I need to convert it for this format:
2021-05-25T03:03:10.000Z
Since I don't have a pojo for that object, it's coming from a jsonArray, I'm struggling to get a Datetime from it.
I tried ObjectMapper, JSON(GSON) and still no success.
Does anyone have a clue how can I do it?
edit.: It's a list of Recordings that I converted to JsonArray (to manipulate, adding another field into the json). When I convert the list to jsonArray, that field (type DateTime) becomes serialized and I need it back to the correct format to pass it forward.
Recording.class;
private final DateTime dateCreated;
List<Recording> listRecordings;
JSONArray jsonArray = new JSONArray(listRecordings);
jsonArray ->
[
{
"offset": 179161383840,
"type": "AUDIO",
"duration": 232,
"codec": "OPUS",
"dateCreated": {
"year": 2021,
"dayOfYear": 145,
"equalNow": false,
"weekyear": 2021,
"chronology": {
"zone": {
"ID": "UTC"
}
},
"weekOfWeekyear": 21,
"secondOfMinute": 10,
"millisOfDay": 10990000,
"monthOfYear": 5,
"dayOfWeek": 2,
"beforeNow": true,
"minuteOfDay": 183,
"dayOfMonth": 25,
"era": 1,
"zone": {
"ID": "UTC"
},
"yearOfCentury": 21,
"centuryOfEra": 20,
"hourOfDay": 3,
"secondOfDay": 10990,
"millis": 1621911790000,
"yearOfEra": 2021,
"minuteOfHour": 3,
"millisOfSecond": 0,
"afterNow": false
},
"size": 521517,
"containerFormat": "MKA",
"status": "COMPLETED"
}
]
I need to revert back dateCreated for the original format: 2021-05-25T03:03:10.000Z
Edit3.:
My workaround:
for (Recording recording : listRecordings) {
Object jsonObject = JSONObject.wrap(recording);
((JSONObject) jsonObject).put("dateCreated", recording.getDateCreated());
jsonArray.put(jsonObject);
}
Best Regards,

Is it possible to turn an array of objects into a map during deserialisation using Gson?

Using the Steam API found here api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/ I get a JSON string like the one below. As you can see, it contains an array of item objects. What I'd like to do is to turn this array into a Map where the defindex is the key and the value is the corresponding item object at point of deserialisation using GSON.
Is this possible or do I have to do that after it's created the objects and populated the array?
"result": {
"status": 1,
"num_backpack_slots": 800,
"items": [
{
"id": 12222222,
"original_id": 333333333,
"defindex": 45,
"level": 10,
"quality": 3,
"inventory": 2147483922,
"quantity": 1,
"origin": 0,
"style": 0,
"attributes": [
{
//...
]
},
{
"id": 3332222222,
"original_id": 554545465,
"defindex": 116,
"level": 10,
"quality": 6,
"inventory": 2147483865,
"quantity": 1,
"origin": 0,
"equipped": [
{
"class": 6,
"slot": 7
},
{
"class": 8,
"slot": 7
}
]
,
"style": 1,
"attributes": [
//...
]
},
{
"id": 4658518468,
"original_id": 897545164648,
"defindex": 130,
"level": 5,
"quality": 3,
"inventory": 2147484134,
"quantity": 1,
"origin": 0,
"attributes": [
{
//...
]
}
]

How do I get sum of a field in solr 4.8

This is my response data:
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"total_amount": 10,
"id": "2"
},
{
"total_amount": 10,
"id": "1"
}
]
}
I want to get sum of total_amount. I tried facet query also. But I did't get sum. I got some blog on this but that is for solr 5.1. http://yonik.com/solr-facet-functions/
You can use the stats functionality to get this information. Just put the followed parameters in your query:
stats=true&stats.field=total_amount
Your response will be like that:
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"id": "1",
"total_amount": 15
},
{
"id": "2",
"total_amount": 12
}
]
},
"stats": {
"stats_fields": {
"total_amount": {
"min": 12,
"max": 15,
"count": 2,
"missing": 0,
"sum": 27,
"sumOfSquares": 369,
"mean": 13.5,
"stddev": 2.1213203435596424,
"facets": {}
}
}
Note that you have lots of information around the total_amount field including the sum.

How to return just the matched elements from a mongoDB array

I've been looking for this question one week and I can't understand why it still don't work...
I have this object into my MongoDB database:
{
produc: [
{
cod_prod: "0001",
description: "Ordenador",
price: 400,
current_stock: 3,
min_stock: 1,
cod_zone: "08850"
},
{
cod_prod: "0002",
description: "Secador",
price: 30,
current_stock: 10,
min_stock: 2,
cod_zone: "08870"
},
{
cod_prod: "0003",
description: "Portatil",
price: 500,
current_stock: 8,
min_stock: 4,
cod_zone: "08860"
},
{
cod_prod: "0004",
description: "Disco Duro",
price: 100,
current_stock: 20,
min_stock: 5,
cod_zone: "08850"
},
{
cod_prod: "0005",
description: "Monitor",
price: 150,
current_stock: 0,
min_stock: 2,
cod_zone: "08850"
}
]
}
I would like to query for array elements with specific cod_zone ("08850") for example.
I found the $elemMatch projection that supposedly should return just the array elements which match the query, but I don't know why I'm getting all object.
This is the query I'm using:
db['Collection_Name'].find(
{
produc: {
$elemMatch: {
cod_zone: "08850"
}
}
}
);
And this is the result I expect:
{ produc: [
{
cod_prod: "0001",
denominacion: "Ordenador",
precio: 400,
stock_actual: 3,
stock_minimo: 1,
cod_zona: "08850"
},{
cod_prod: "0004",
denominacion: "Disco Duro",
precio: 100,
stock_actual: 20,
stock_minimo: 5,
cod_zona: "08850"
},
{
cod_prod: "0005",
denominacion: "Monitor",
precio: 150,
stock_actual: 0,
stock_minimo: 2,
cod_zona: "08850"
}]
}
I'm making a Java program using MongoDB Java Connector, so I really need a query for java connector but I think I will be able to get it if I know mongo query.
Thank you so much!
This is possible through the aggregation framework. The pipeline passes all documents in the collection through the following operations:
$unwind operator - Outputs a document for each element in the produc array field by deconstructing it.
$match operator will filter only documents that match cod_zone criteria.
$group operator will group the input documents by a specified identifier expression and applies the accumulator expression $push to each group:
$project operator then reconstructs each document in the stream:
db.collection.aggregate([
{
"$unwind": "$produc"
},
{
"$match": {
"produc.cod_zone": "08850"
}
},
{
"$group":
{
"_id": null,
"produc": {
"$push": {
"cod_prod": "$produc.cod_prod",
"description": "$produc.description",
"price" : "$produc.price",
"current_stock" : "$produc.current_stock",
"min_stock" : "$produc.min_stock",
"cod_zone" : "$produc.cod_zone"
}
}
}
},
{
"$project": {
"_id": 0,
"produc": 1
}
}
])
will produce:
{
"result" : [
{
"produc" : [
{
"cod_prod" : "0001",
"description" : "Ordenador",
"price" : 400,
"current_stock" : 3,
"min_stock" : 1,
"cod_zone" : "08850"
},
{
"cod_prod" : "0004",
"description" : "Disco Duro",
"price" : 100,
"current_stock" : 20,
"min_stock" : 5,
"cod_zone" : "08850"
},
{
"cod_prod" : "0005",
"description" : "Monitor",
"price" : 150,
"current_stock" : 0,
"min_stock" : 2,
"cod_zone" : "08850"
}
]
}
],
"ok" : 1
}

Categories

Resources