How to edit a json file using jsoncons? - c++

I am using jsoncons, which is a requirement for this question.
I am trying to edit some fields in a deeply nested json file, for example the following one:
{
"Error": null,
"HubNotificationId": "8a321f40-3692-11ec-b2a8-02420a0000fc",
"HubNotificationTimestamp": "2021-10-26T19:25:55.330812",
"MessageTimestampUtc": "2021-10-26T19:25:55",
"Result": "SUCCESS",
"ScanId": "0789e64f-87a7-42b0-a912-362acad7536c",
"ScanTimestampUtc": "2021-10-26T19:25:44",
"Source": {
"InstanceId": "",
"ModuleId": "atlas",
"ServiceId": "uv-acquisition-manager",
"ServiceVersion": "2.1.0-0001",
"SiteId": "rnd-oem-holon"
},
"Type": "ScanNotification",
"Version": "2.8.0",
"abortReason": null,
"context": "scan/end",
"data": [
{
"frameGrabberId": "at-uv-dm-fg1",
"scanLocation": {
"Original_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Original_ScaledDown",
"uri": "INJECTED"
},
"Undistorted_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Undistorted_ScaledDown",
"uri": "INJECTED"
},
"Unprocessed": {
"keyPrefix": "a0366334-b44b-459e-8484-8a0c835c6889/c5424457-9066-4d82-9ab9-df5cc2a17c21/20201006/2020-10-06T11-16-04.000Z_98021622-12ff-455b-972d-79af24c169a1",
"mimeType": "",
"role": "Unprocessed",
"uri": "INJECTED"
}
}
},
{
"frameGrabberId": "at-uv-dm-fg2",
"scanLocation": {
"Original_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Original_ScaledDown",
"uri": "INJECTED"
},
"Undistorted_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Undistorted_ScaledDown",
"uri": "INJECTED"
},
"Unprocessed": {
"keyPrefix": "INJECTED",
"mimeType": "",
"role": "Unprocessed",
"uri": "INJECTED"
}
}
},
{
"frameGrabberId": "at-uv-dm-fg3",
"scanLocation": {
"Original_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Original_ScaledDown",
"uri": "INJECTED"
},
"Undistorted_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Undistorted_ScaledDown",
"uri": "INJECTED"
},
"Unprocessed": {
"keyPrefix": "a0366334-b44b-459e-8484-8a0c835c6889/c5424457-9066-4d82-9ab9-df5cc2a17c21/20201006/2020-10-06T11-16-04.000Z_98021622-12ff-455b-972d-79af24c169a1",
"mimeType": "",
"role": "Unprocessed",
"uri": "INJECTED"
}
}
},
{
"frameGrabberId": "at-uv-dm-mgr",
"scanLocation": {
"Original_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Original_ScaledDown",
"uri": "INJECTED"
},
"Undistorted_ScaledDown": {
"keyPrefix": null,
"mimeType": "",
"role": "Undistorted_ScaledDown",
"uri": "INJECTED"
},
"Unprocessed": {
"keyPrefix": "a0366334-b44b-459e-8484-8a0c835c6889/c5424457-9066-4d82-9ab9-df5cc2a17c21/20201006/2020-10-06T11-16-04.000Z_98021622-12ff-455b-972d-79af24c169a1",
"mimeType": "",
"role": "Unprocessed",
"uri": "INJECTED"
}
}
}
],
"dynamicMetaData": {
"vehicleParams": {
"color": "122",
"model": "225",
"vendor": "a"
}
},
"scanDuration_sec": 10.98,
"shotsCount": 96,
"sourceInfo": [],
"staticMetaData": null
}
I want to edit some of the keys - for example, the uri keys.
How to use the library to be able to edit the json and save an edited copy?
What I have so far is
const jsoncons::json& TestMessage::injectPathToFMTemplate(const std::string &scanPath) {
auto& raw = this->_raw_input;
auto& parsed = this->_templateMessage;
auto parsedTree = parsed.as<std::map<std::string, jsoncons::json>>();
auto dataPos = parsedTree.find("data");
auto& data = dataPos->second;
auto grabbers = data.as<std::vector<std::map<std::string, jsoncons::json>>>();
int grabberIndex = 0;
for (auto& grabberTree : grabbers){
auto scanLocationPos = grabberTree.find("scanLocation");
auto scanLocationTree = scanLocationPos->second.as<std::map<std::string, jsoncons::json>>();
auto Original_ScaledDownPos = scanLocationTree.find("Original_ScaledDown");
auto Undistorted_ScaledDownPos = scanLocationTree.find("Undistorted_ScaledDown");
auto UnprocessedPos = scanLocationTree.find("Unprocessed");
auto Original_ScaledDownPosTree = Original_ScaledDownPos->second.as<std::map<std::string, jsoncons::json>>();
auto Undistorted_ScaledDownPosTree = Original_ScaledDownPos->second.as<std::map<std::string, jsoncons::json>>();
auto UnprocessedPosTree = Original_ScaledDownPos->second.as<std::map<std::string, jsoncons::json>>();
auto uriPos1 = Original_ScaledDownPosTree.find("uri");
auto uriPos2 = Undistorted_ScaledDownPosTree.find("uri");
auto uriPos3 = UnprocessedPosTree.find("uri");
uriPos1->second = scanPath;
uriPos2->second = scanPath;
uriPos3->second = scanPath;
++grabberIndex;
}
parsed = jsoncons::json(parsedTree);
auto s = parsed.as<std::string>();
return parsed;
Which goes all the way down to the uri fields, but then can't edit them because nothing is (or can be) by reference, and I find that I have to re-build all of the json object again, which makes no sense.
I am sure the library allows this somehow, just can't find out how.

The jsoncons library provides a number of options for editing a JSON value, but a simple way is
std::string scanPath = "FOO";
try
{
json root = json::parse(input);
json& data = root.at("data");
for (auto& grabberTree : data.array_range())
{
json& scanLocationTree = grabberTree.at("scanLocation");
json& Original_ScaledDown = scanLocationTree.at("Original_ScaledDown");
json& Undistorted_ScaledDown = scanLocationTree.at("Undistorted_ScaledDown");
json& Unprocessed = scanLocationTree.at("Unprocessed");
Original_ScaledDown.at("uri") = scanPath;
Undistorted_ScaledDown.at("uri") = scanPath;
Unprocessed.at("uri") = scanPath;
}
std::cout << pretty_print(root) << "\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}

Related

How to set or not set audio selector in AWS media convert?

I got this error, when creating job for AWS media convert:
Invalid selector_sequence_id [0] specified for audio_description [1].
I do not even need sound for my output mp4 video.
My intention is to loop for 2 second an image (png or jpg) and add a fade effect for the first frames.
How would you change the sent json?
{
"middlewareStack": {},
"input": {
"Queue": "arn:aws:mediaconvert:eu-central-1:634617701827:queues/Default",
"UserMetadata": {},
"Role": "arn:aws:iam::634617701827:role/service-role/MediaConvert_Default_Role",
"Settings": {
"TimecodeConfig": {
"Anchor": "00:00:00:00",
"Source": "EMBEDDED"
},
"OutputGroups": [
{
"Name": "File Group",
"Outputs": [
{
"Preset": "createPromoVideo",
"Extension": "mp4",
"NameModifier": "_fade",
"VideoDescription": {
"CodecSettings": {
"FilterGraph": "fade=out:150:30"
},
"ScalingBehavior": "DEFAULT",
"TimecodeInsertion": "DISABLED",
"AntiAlias": "ENABLED",
"Sharpness": 50,
"Height": 1080,
"Width": 1080
},
"AudioDescriptions": [
{
"AudioSelector": {
"SelectorSettings": [
{
"AudioSelectorName": "Default"
}
]
},
"CodecSettings": {
"Codec": "AAC",
"AacSettings": {
"Bitrate": 96000,
"CodingMode": "CODING_MODE_2_0",
"SampleRate": 48000
}
}
}
]
}
],
"OutputGroupSettings": {
"Type": "FILE_GROUP_SETTINGS",
"FileGroupSettings": {
"Destination": "s3://t44-post-cover/8fui.mp4",
"DestinationSettings": {
"S3Settings": {
"AccessControl": {
"CannedAcl": "PUBLIC_READ"
}
}
}
}
}
}
],
"Inputs": [
{
"FileInput": "s3://t44-post-cover/8fui",
"VideoSelector": {
"ColorSpace": "FOLLOW"
},
"FilterEnable": "AUTO",
"TimecodeSource": "ZEROBASED",
"InputClippings": [
{
"StartTimecode": "00:00:00:00",
"EndTimecode": "00:00:02:00"
}
],
"FilterGraph": "fade=in:0:30",
"AudioSelectors": {
"Default": {
"DefaultSelection": "DEFAULT"
}
}
}
]
},
"AccelerationSettings": {
"Mode": "DISABLED"
},
"StatusUpdateInterval": "SECONDS_60",
"Priority": 0
}
}
AWS MediaConvert requires you to have at least one Audio Selector.
Just provide it with this simple one:
"Inputs": [
...
{
"AudioSelectors": {
"Audio Selector 1": {
"Offset": 0,
"DefaultSelection": "DEFAULT",
"SelectorType": "LANGUAGE_CODE",
"ProgramSelection": 1,
"LanguageCode": "ENM"
}
},
...
},
UPDATE:
A more barebones one:
"Inputs": [
...
{
"AudioSelectors": {
"Audio Selector 1": {
DefaultSelection: 'DEFAULT',
},
}
},
...
},

AWS DynamoDB batchWriteItem in Lamda is not working properly

I have an app created via AWS Amplify and I created an AWS Lambda function that aim to insert multiple data to one of the table in DynamoDB.
Firstly, I tried map attributes to documentClient.put({...params}).promise() and run Promise.all() on it. But some items got lost. For example, I add 40 items at once, but only 5-10 got added. I thought this could be some lambda limit issue. So I switch to batchWriteItem() and seems it remain the same or even worse (only 1 out of 40 got added). Here is the code I wrote:
export const addAvailabilities = async (
docClient: AWS.DynamoDB.DocumentClient,
newAvailabilities: Availability[],
expertId: string,
cognitoUserId: string
) => {
console.info('#addAvailabilities: Start')
try {
let restArray = [...newAvailabilities]
const promiseArray = []
while (restArray.length > 0) {
const executingArray = restArray.slice(0, 25)
const temp = batchAddAvailability(docClient, executingArray, expertId, cognitoUserId)
promiseArray.push(temp)
restArray = restArray.slice(25)
}
const result = await Promise.all(promiseArray)
console.info(result)
console.info('#addAvailabilities: End')
return result
} catch (err) {
console.error('#addAvailabilities: Error')
throw err
}
}
const mapBatchAddAvailabilityParams = (newAvailabilities: Availability[], expertId: string, cognitoUserId: string) => {
return newAvailabilities.map((availability, index) => {
const currentTime = `${moment().utc().format('YYYY-MM-DD[T]HH:mm:ss.SS')}${index}Z`
return {
PutRequest: {
Item: {
id: uuid(),
__typename: 'ExpertAvailability',
expertId: expertId,
owner: cognitoUserId,
startTime: availability.start,
status: 'available',
createdAt: currentTime,
updatedAt: currentTime
}
}
}
})
}
const batchAddAvailability = async (
docClient: AWS.DynamoDB.DocumentClient,
newAvailabilities: Availability[],
expertId: string,
cognitoUserId: string
) => {
console.info('#batchAddAvailability: Start')
try {
const batchParams = mapBatchAddAvailabilityParams(newAvailabilities, expertId, cognitoUserId)
const param = {
RequestItems: {
[process.env.API_TAP_EXPERTAVAILABILITYTABLE_NAME]: batchParams
}
}
console.info('params', JSON.stringify(param))
return docClient.batchWrite(param).promise()
} catch (err) {
console.error('#batchAddAvailability: Error')
throw err
}
}
I add this const currentTime = ${moment().utc().format('YYYY-MM-DD[T]HH:mm:ss.SS')}${index}Z` because I saw this post has similar issue as mine and that's the solution solve his problem. But it doesn't solve mine.
From the logs, params get format correctly like this:
{
"RequestItems": {
"Availability": [
{
"PutRequest": {
"Item": {
"id": "66a7b63e-a14b-4ba2-94a9-0dd7bf457efe",
"__typename": "ExpertAvailability",
"expertId": "8d30c1a4-685e-40bf-b884-6e50bb422e99",
"owner": "test-keycloak_eeb71cb1-6c11-4fb9-a721-ce5dc7d06269",
"startTime": "2021-10-03T23:00:00.000Z",
"status": "available",
"createdAt": "2021-09-08T10:24:28.880Z",
"updatedAt": "2021-09-08T10:24:28.880Z"
}
}
},
{
"PutRequest": {
"Item": {
"id": "162d839d-7fde-417e-994b-2dc12336c4cf",
"__typename": "ExpertAvailability",
"expertId": "8d30c1a4-685e-40bf-b884-6e50bb422e99",
"owner": "test-keycloak_eeb71cb1-6c11-4fb9-a721-ce5dc7d06269",
"startTime": "2021-10-04T00:00:00.000Z",
"status": "available",
"createdAt": "2021-09-08T10:24:28.881Z",
"updatedAt": "2021-09-08T10:24:28.881Z"
}
}
},
{
"PutRequest": {
"Item": {
"id": "dc257c75-9a27-482a-88c5-1747ffe97361",
"__typename": "ExpertAvailability",
"expertId": "8d30c1a4-685e-40bf-b884-6e50bb422e99",
"owner": "test-keycloak_eeb71cb1-6c11-4fb9-a721-ce5dc7d06269",
"startTime": "2021-10-04T01:00:00.000Z",
"status": "available",
"createdAt": "2021-09-08T10:24:28.932Z",
"updatedAt": "2021-09-08T10:24:28.932Z"
}
}
},
{
"PutRequest": {
"Item": {
"id": "75b2e911-e842-4f11-99ed-702c6cf1c485",
"__typename": "ExpertAvailability",
"expertId": "8d30c1a4-685e-40bf-b884-6e50bb422e99",
"owner": "test-keycloak_eeb71cb1-6c11-4fb9-a721-ce5dc7d06269",
"startTime": "2021-10-04T02:00:00.000Z",
"status": "available",
"createdAt": "2021-09-08T10:24:28.943Z",
"updatedAt": "2021-09-08T10:24:28.943Z"
}
}
},
{
"PutRequest": {
"Item": {
"id": "df65e151-1699-446d-ab3b-06aca707a2fb",
"__typename": "ExpertAvailability",
"expertId": "8d30c1a4-685e-40bf-b884-6e50bb422e99",
"owner": "test-keycloak_eeb71cb1-6c11-4fb9-a721-ce5dc7d06269",
"startTime": "2021-10-04T03:00:00.000Z",
"status": "available",
"createdAt": "2021-09-08T10:24:28.944Z",
"updatedAt": "2021-09-08T10:24:28.944Z"
}
}
},
{
"PutRequest": {
"Item": {
"id": "98a5bdd2-60d3-4d5d-9913-40b5c84c6d62",
"__typename": "ExpertAvailability",
"expertId": "8d30c1a4-685e-40bf-b884-6e50bb422e99",
"owner": "test-keycloak_eeb71cb1-6c11-4fb9-a721-ce5dc7d06269",
"startTime": "2021-10-04T04:00:00.000Z",
"status": "available",
"createdAt": "2021-09-08T10:24:28.945Z",
"updatedAt": "2021-09-08T10:24:28.945Z"
}
}
}
]
}
}
There is no UnprocessedItems or any errors return from the execution. But the items are just missing. The capacity of the table is on-demand so I think capacity shouldn't be a problem. Any ideas what's wrong? Many thanks

Parity POA: the validator's are not getting paid in ETH for sealing blocks

I am working on creating Parity private Blockchain, however,the validator's are not getting paid in ETH for sealing blocks.
I use this command line to check the balance:
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x0037a6b811ffeb6e072da21179d11b1406371c63", "latest"],"id":1}' http://172.0.0.1:8545
I want to ask how can I fix these problem.
{
"name": "Testnet",
"engine": {
"authorityRound": {
"params": {
"gasLimitBoundDivisor": "0x400",
"stepDuration": "2",
"validators" : {
"list": [ "0xa19b0e4f7ba1d5f74960c0aad794756a0a16eab4", "0x9c8f23e0a9377bd98322f8333142eadbaed200e8", "0x2f2033e303d4bf17403521e0c1830bac4ba09323", "0xe883b46f02ecd0e624082fe6ff12af0337ba0cde", "0xbdc56eb866933e7ba827fa293d4545ef2a350ce2"]
}
}
}
},
"params": {
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"gasLimitBoundDivisor": "0x400",
"networkID" : "0x11"
},
"genesis": {
"seal": {
"authorityRound": {
"step": "0x0",
"signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
}
},
"difficulty": "0x20000",
"gasLimit": "0x1312D00"
},
"accounts": {
"0x0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
"0x0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
"0x0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
"0x0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
}
}
You are querying the wrong balance:
0x0037a6b811ffeb6e072da21179d11b1406371c63 is not in your validator node list.
You only specified the following validators:
"list": [
"0xa19b0e4f7ba1d5f74960c0aad794756a0a16eab4",
"0x9c8f23e0a9377bd98322f8333142eadbaed200e8",
"0x2f2033e303d4bf17403521e0c1830bac4ba09323",
"0xe883b46f02ecd0e624082fe6ff12af0337ba0cde",
"0xbdc56eb866933e7ba827fa293d4545ef2a350ce2"
]
Try to get the balance of these nodes.

Hypen in response data is throwing error when setting as global variable for nested response

I have a post man request. Which returns a response data as:
[
{
"id": "7ca27c09-2b67-417e-b367-f97d49824a2f",
"tags": [],
"type": "ApplicationProfile",
"userId": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
"profile": {
"User-Profile": {
"PHI": {
"gender": "Male",
"surgeryDate": {
"value": "2017-06-06"
},
"surgeryType": {
"value": "gastricBand"
},
"heightInches": "72",
"approvalPhase": "2",
"profileImageUrl": "",
"motivationalImageUrl": ""
},
"PII": {
"mail": "c11#c111.com",
"lastName": "",
"firstName": "name1",
"shouldReceiveNotifications": true
}
},
"motivationalGoals": [
{
"goal": "Improving health",
"goalId": "2957"
},
{
"goal": "Relieving pain",
"goalId": "2958"
},
{
"goal": "Feeling better",
"goalId": "2960"
}
]
},
"version": 35,
"createdBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
"lastModifiedBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
"applicationName": "Health Partner",
"createdDateTime": "2017-06-08T16:08:32.497Z",
"lastModifiedDateTime": "2017-06-09T13:23:03.503Z"
},
{
"id": "091b5ebd-b096-436e-a703-f97f0ae77baf",
"tags": [
"Application-Profile"
],
"type": "ApplicationProfile",
"userId": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
"profile": {
"User-Profile": {
"PHI": {
"gender": "",
"surgeryDate": {
"value": "2017-06-01"
},
"surgeryType": {
"value": ""
},
"heightInches": "72",
"approvalPhase": "2",
"profileImageUrl": "",
"motivationalImageUrl": ""
},
"PII": {
"mail": "c11#c111.com",
"lastName": "Dev",
"firstName": "Kat",
"shouldReceiveNotifications": true
}
},
"motivationalGoals": [
{
"goal": "Improving health",
"goalId": "2957"
},
{
"goal": "Relieving pain",
"goalId": "2958"
},
{
"goal": "Feeling better",
"goalId": "2960"
}
]
},
"version": 41,
"createdBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
"lastModifiedBy": "2f2c8684-874d-49ea-bfa4-977069c1e3e2",
"applicationName": "Health Partner Weightloss",
"createdDateTime": "2017-06-03T16:19:57.811Z",
"lastModifiedDateTime": "2017-06-08T16:07:49.555Z"
}
]
I need to extract the value of the emailid and set as global variable.
here is my code:
var jsonData1 = JSON.parse(responseBody);
postman.setGlobalVariable("jsonData1",jsonData1.profile.User-Profile.PII.mail);
But I am getting error. "User is undefined".
The problem here is that the hyphen / minus sign is interpreted as the subtraction operator.
Consider changing your API and using "userProfile" instead of "User-Profile".

c++ json-nlohmann accessing the list elements

I'm trying to use json-nlohmann library to read JSON files in C++
So far I got along with it preatty well, but now I'm trying to access elements of the list in given json. JSON:
{
"GameMap": "path_to_game_map",
"Objects": [
{ "object_1": { "Transform": { "Position": { "X": 1, "Y": 2 }, "Rotation": { "X": 3.5, "Y": 8.2 }, "Scale": { "X": 1, "Y":1 } },
"Components": [
{ "0": { "data": { "some_data": "false" } } },
{ "1": { "data": { "some_data": "false" } } },
{ "2": { "data": { "some_data": "false" } } }
] } },
{ "object_2": { "Transform": { "Position": { "X": 1, "Y": 2 }, "Rotation": { "X": 3.5, "Y": 8.2}, "Scale": { "X": 1, "Y":1 } },
"Components": [
{ "0": { "data": { "some_data": "false" } } },
{ "1": { "data": { "some_data": "false" } } },
{ "2": { "data": { "some_data": "false" } } }
] } }
]
}
Where I'm trying to access each Component and read it's key value.
I've got an object out for every Component. But I just cannot figure out how to read its key and value.
Help! Please.
When you iterate a JSON object, say j, you can access the key and value like this:
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
}
you can also now use for (auto& element : j.items()) and then element.key() and element.value() inside the loop.