How to mask values in complex json structure using dataweave? - mule-studio

I want to mask the element in the json. descID element in the below json should be masked. Could you please suggest.
{
"status": "ok",
"statusCode": "19x9s011",
"statusDescription": "Service: XYZ IOP ; country: india ; Locale:en-US ; SourceId:KOP; ApiUid: 644e1dd7-2a7f-18fb-b8ed-ed78c3f899s2c2b; Description: The NMK profile call was successful.",
"details": {
"descID": "11840000000012698",
"Code": "XX",
"languageCode": "en",
"profile": {
"base": {
"username": "abc",
"firstName": "xc",
"middleName": "test",
"lastName": "123",
"shortName": "xc",
"displayName": "D",
"suffix": "T",
"prefix": "E"
}
}
}
}

I might use something like to mask the data:
%dw 1.0
%input payload application/json
%output application/json
%var keyToEncrypt = ['descID']
%function encrypt(val) "*****"
%function needsEncrypt(key) (sizeOf (keyToEncrypt find key)) > 0
%function maskSensitiveData(value) value mapObject ({
($$) : maskSensitiveData($) when $ is :object otherwise $
} unless needsEncrypt($$ as :string) otherwise {
($$) : encrypt($)
})
---
maskSensitiveData(payload)
This is taken verbatim from https://blogs.mulesoft.com/dev/training-dev/encrypt-specific-xml-tags-with-the-power-of-dataweave/
If you need to remove the field altogether, then I might use something like:
%dw 1.0
%input payload application/json
%output application/json
%var keyToEncrypt = ['descID']
%function encrypt(val) "*****"
%function needsEncrypt(key) (sizeOf (keyToEncrypt find key)) > 0
%function maskSensitiveData(value) value mapObject ({
($$) : maskSensitiveData($) when $ is :object otherwise $
} unless needsEncrypt($$ as :string) otherwise {})
---
maskSensitiveData(payload)

1.Convert payload to object
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
2.Overwrite descID in payload
<expression-component doc:name="Expression"><![CDATA[payload.details.descID=flowVars.maskedvalue;]]></expression-component>
3.Convert back to json
<json:object-to-json-transformer doc:name="Object to JSON"/>

Related

How to extract values from json string from api in scala?

I am trying to extract specific value from each json in a response from api.
for example if I have http response is kind of string array as below:
[
{
"trackerType": "WEB",
"id": 1,
"appId": "ap-website",
"host": {
"orgId": "ap",
"displayName": "AP Mart",
"id": "3",
"tenantId": "ap"
}
},
{
"trackerType": "WEB",
"id": 2,
"appId": "test-website",
"host": {
"orgId": "t1",
"tenantId": "trn11"
}
}
]
I wanted to extract or keep only list of values app_id and tenant_id as below:
[
{
"appId": "ap-website",
"tenantId": "ap"
},
{
"appId": "test-website",
"tenantId": "trn11"
}
]
If your HTTP response is quite big and you wouldn't hold it all in memory then consider using IO streams for parsing the body and serialization of the result list.
Below is an example of how it can be done with the dijon library.
Add dependency to your build file:
libraryDependencies += "me.vican.jorge" %% "dijon" % "0.5.0+18-46bbb74d", // Use %%% instead of %% for Scala.js
Import following packages:
import com.github.plokhotnyuk.jsoniter_scala.core._
import dijon._
import scala.language.dynamics._
Parse your input stream transforming it value by value in the callback to the output stream:
val in = new java.io.ByteArrayInputStream(
"""
[
{
"trackerType": "WEB",
"id": 1,
"appId": "ap-website",
"host": {
"orgId": "ap",
"displayName": "AP Mart",
"id": "3",
"tenantId": "ap"
}
},
{
"trackerType": "WEB",
"id": 2,
"appId": "test-website",
"host": {
"orgId": "t1",
"tenantId": "trn11"
}
}
]
""".getBytes("UTF-8"))
val out = new java.io.BufferedOutputStream(System.out)
out.write('[')
scanJsonArrayFromStream[SomeJson](in) {
var writeComma = false
x =>
if (writeComma) out.write(',') else writeComma = true
val json = obj("appId" -> x.appId, "tenantId" -> x.host.tenantId)
writeToStream[SomeJson](json, out)(codec)
true
} (codec)
out.write(']')
out.flush()
You can try it with Scastie here
When using this code in your application, you need to replace the source and destination of input and output streams.
There are other options how to solve your task. Please add more context that will help us in selection of the most simple and efficient solution.
Feel free to comment - I will be happy to help you in tuning the solution to your needs.

Getting extra variable in XML to JSON conversion using perl

My Program is giving extra variable $t in output. Can anyone help me on this?
use XML::XML2JSON;
xml content
my $XML = '<file><sno>1</sno><process>VALID</process><validation_type>C</validation_type><file_type>HTML</file_type><line>2</line><column>78</column><status>0</status><type>Warning</type><code>001</code><rule>aligning content.</rule><desc>Check that non-breaking space.</desc></file>';
my $XML2JSON = XML::XML2JSON->new();
my $JSON = $XML2JSON->convert($XML);
print $JSON;
Output - Extra variable is coming $t
{
"#encoding": "UTF-8",
"#version": "1.0",
"file": {
"status": {
"$t": "0"
},
"rule": {
"$t": "aligning content."
},
"validation_type": {
"$t": "C"
},
"process": {
"$t": "VALID"
},
"sno": {
"$t": "1"
},
"file_type": {
"$t": "HTML"
},
"desc": {
"$t": "Check that non-breaking space."
},
"type": {
"$t": "Warning"
},
"code": {
"$t": "001"
},
"line": {
"$t": "2"
},
"column": {
"$t": "78"
}
}
}
Expected output is:
{
"sno": "1",
"process": "VALID",
"validation_type": "C",
"file_type": "HTML",
"line": "2",
"column": "78",
"status": "0",
"type": "Warning",
"code": "001",
"rule": "aligning content.",
"desc": "Check that non-breaking space."
}
The $t is content key as mentioned in the XML::XML2JSON documentation.
If your intention is to convert from XML to JSON, I would recommend to use XML::Simple and later you can encode using JSON.pm.
Code below:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use XML::Simple;
#Create an object
my $xmlSimple = new XML::Simple;
my $XML = '<file><sno>1</sno><process>VALID</process><validation_type>C</validation_type><file_type>HTML</file_type><line>2</line><column>78</column><status>0</status><type>Warning</type><code>001</code><rule>aligning content.</rule><desc>Check that non-breaking space.</desc></file>';
#either we can pass a variable or a xml file which contains xml data
my $dataXML = $xmlSimple->XMLin($XML);
my $jsonString = encode_json($dataXML);
print $jsonString;
Output:
{
"process":"VALID",
"line":"2",
"column":"78",
"type":"Warning",
"file_type":"HTML",
"sno":"1",
"status":"0",
"rule":"aligning content.",
"code":"001",
"desc":"Check that non-breaking space.",
"validation_type":"C"
}

JSON API .Net Core Put and Patch Examples

I am testing boilerplate library for dotnet core with json:api specification from github repo {json:api}. The endpoints for GET (with or without query), POST & DELETE are working as expected when I send from postman. But I couldn't find working examples to change the existing resource with PUT or PATCH. When i send patch request with data, it give me back response "200 OK" but it didn't change in database. Below are my request and response.
Request GET : http://localhost:5000/api/people -> 200 OK
Response : [
{
"name": "Samuel",
"articles": null,
"id": 2,
"stringId": "2"
},
{
"name": "John",
"articles": null,
"id": 3,
"stringId": "3"
},
{
"name": "Robbin",
"articles": null,
"id": 4,
"stringId": "4"
} ]
Request GET: http://localhost:5000/api/people/2 -> 200 OK
Response : {
"name": "Samuel",
"articles": null,
"id": 2,
"stringId": "2"
}
Request GET: http://localhost:5000/api/people/2?include=articles -> 200 OK
Response : {
"name": "Samuel",
"articles": [],
"id": 2,
"stringId": "2"
}
Request POST: http://localhost:5000/api/people -> 201 Created
Request Body: {"name":"Samuel"}
Response : {
"name": "Samuel",
"articles": null,
"id": 2,
"stringId": "2"
}
Request DELETE: http://localhost:5000/api/people/2 -> 204 No Content
How can I update data?
I made a final decision after reading specification documents of JSONAPI and OData. I will just stick to my own format for better understanding of my own code and I recommend Swagger for Api Documentation. It doesn't make sense if the spec doesn't meet my requirement even when people are telling it's the standard.
I found in documents that require to includes following two headers for different api calls and body request is also different for PATCH.
"Accept: application/vnd.api+json" <--- This needs to put in header
"Content-Type: application/vnd.api+json" <--- This also needed.
Request PATCH: http://localhost:5000/api/people/3 -> 200 OK
// Request body becomes text, anybody knows how to format to JSON?
Request Body(Text): {
"data": {
"type": "people",
"attributes": {
"name": "John"
}
}
}
Response : {
"data": {
"attributes": {
"name": "John"
},
"relationships": {
"articles": {
"links": {
"self":
"http://localhost:5000/api/people/3/relationships/articles",
"related": "http://localhost:5000/api/people/3/articles"
}
}
},
"type": "people",
"id": "3"
} }

fiware-orion "code" : "400", "details" : "invalid payload: unknown fields"

I've created an Orion instance based on orion-psb-image-R4.2 instance in FIWARE Lab, just updated (orion, pep, cygnus) after setting it. It is listening on internet and I have verified connevity and so.
I tried to create a simple instance with this query:
POST http:x.y.w.z:1026/v1/contextEntities/
Headers:
Content-Type: application/json
Accept: application/json
Body:
{
"id": "Sala1",
"type": "Sala",
"Attributes": [
{
"name": "temperatura",
"type": "float",
"value": "20"
},
{
"name": "humedad",
"type": "float",
"value": "80"
}
]
}
The answer received from the Web server is:
200, OK
Date: Fri, 05 Feb 2016 10:34:51 GMT
Content-Length: 132
Content-Type: application/json
But the answer from ORION is:
{
"errorCode": {
"code": "400",
"reasonPhrase": "Bad Request",
"details": "invalid payload: unknown fields"
}
}
Any idea on what I'm missing?.
Thanks & Regards.
EDIT: Sorry... that's something I already fixed. Somehow I copied it wrongly.
The previous error was "json syntax error".
That's fixed.
The error I reported remains after writing "attributes" with lower case "a".
Any other idea?
POST: HTTP://x.y.w.z:1026/v1/contextEntities/
HEADERS
Content-Type: application/json
Accept: application/json
BODY
{
"id": "Sala1",
"type": "Sala",
"attributes": [
{
"name": "temperature",
"type": "float",
"value": "20"
},
{
"name": "humidity",
"type": "float",
"value": "80"
}
]
}
Thanks in advance
Orion API is case sensitive. Thus, probably the problem is that your payload uses Attributes instead of attributes (i.e. lower-case a).

DocuSign Request Signature by providing a pre-filled pdf document, while referencing the template id in DocuSign which defines the signature tab

I have a PDF document stored as Docusign Template that just defines a signature tab and no other information is filled in that PDF. From my application, I will be providing the same PDF with information filled in. Is it possible to request signature from my client providing the pre-filled PDF and reference the Docusign Template, so the signature tab shows up with the pre-filled data?
I am not sure if there is an API that supports this requirement. I looked at using composite templates, but it did not work as I expected, may be I was using it wrong. Here is the json request:
--BOUNDARY
Content-Type: application/json
Content-Disposition: form-data
--BOUNDARY
Content-Type: application/pdf
Content-Disposition: file; filename="form4506-doe.pdf"; documentId=1
{
"emailSubject": "Sent from a Template",
"templateRoles": [],
"status": "sent",
"compositeTemplates": [
{
"serverTemplates": [
{
"sequence": "1",
"templateId": "10ce17a0-0a25-4485-883c-72c1da059d13"
}
],
"inlineTemplates": [
{
"sequence": "1",
"recipients": {
"editors": [],
"agents": [],
"signers": [
{
"clientUserId": "1",
"recipientId": "1",
"name": "John Doe",
"email": "jdoe#example.com"
}
],
"certifiedDeliveries": [],
"carbonCopies": []
}
}
],
"document": {
"name": "form4506-doe.pdf",
"documentId": "1"
}
}
]
}
--BOUNDARY--
This request sent the pre-filled PDF to the client, but the signature tab is missing (which I expect it to have come from the template). Appreciate any help from DocuSign support.
I think I figured out the working combination. RoleName is required to be set on the Signer object, which shows the Signature tab on the merged template. Sequence needs to be re-ordered. Server template should be set to sequence 2 and inline template should be set to sequence 1. Document id should point to the document id from DocuSign template documents. Here is the working json request:
--BOUNDARY
Content-Type: application/json
Content-Disposition: form-data
{
"emailSubject": "Sent from a Template",
"templateRoles": [],
"status": "sent",
"compositeTemplates": [
{
"compositeTemplateId": "1",
"serverTemplates": [
{
"sequence": "2",
"templateId": "10ce17a0-0a25-4485-883c-72c1da059d13"
}
],
"inlineTemplates": [
{
"sequence": "1",
"recipients": {
"editors": [],
"agents": [],
"signers": [
{
"clientUserId": "1",
"recipientId": "1",
"name": "John Doe",
"email": "jdoe#example.com",
"roleName": "borrower"
}
],
"certifiedDeliveries": [],
"carbonCopies": []
},
"documents": [
{
"name": "form4506-doe.pdf",
"documentId": "98141843"
}
]
}
]
}
]}
--BOUNDARY
Content-Type: application/pdf
Content-Disposition: file; filename="form4506-doe.pdf"; documentId=98141843
<bytes of PDF removed>
--BOUNDARY--