I need to write a RegEx on a JSON to match everything that starts with {$ and ends with }
I tried with /{(.*?)}/g and it seemingly works fine but if you see the image below it also matches the other text so how do I explicitly write a RegEx for my requirement
The reason for the ask is I need to find values with {$*} and replace them with a string
Below is my JSON
{
"name": "{$StubName}",
"request": {
"method": "POST",
"url": "/marks/{$Name}",
"bodyPatterns": [
{
"equalToJson": "{\n \"name\": \"{$RequestName}\",\n \"job\": \"{$Role}\"\n}"
}
]
},
"response": {
"status": "201",
"headers": {
"Content-Type": "application/json"
},
"body": "{\n \"name\": \"{$RequestName}\",\n \"job\": \"{$Role}\",\n \"id\": \"{$id}\",\n \"createdAt\": \"{$Time}\"\n}"
}
}
You can use \{\$\w*\}
Regex demo
Related
Endpoint is POST and request body is an array so it's size can vary. I need to create an wiremock json which can give this array request in response, also i need to do additional of two fields in response.
wiremock is something like this
{
"request": {
"method": "POST",
"urlPathPattern": "/tax/v2/calculateTax"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"jsonBody": [
{
"banId": "{{jsonPath request.body '$[0].banId'}}",
"orderItemInfo": [
{
"id": "{{jsonPath request.body '$[0].orderItemInfo[0].id'}}",
"amount": "{{jsonPath request.body '$[0].orderItemInfo[0].amount'}}",
"tax": "", // 10 % of amount
"amountIncludingTax": "", sum of amount+tax
}
]
}
]
}
}
Here mainly size of orderListInfo will vary. Reponse should have as many orderListInfo as in request dynamically.
Everything should be done in json only and not in java class.
Request can be something like below
[
{
"banId": "ban_01",
"orderItemInfo": [
{
"id": "id_01",
"amount": "100",
},
{
"id": "id_02",
"amount": "275",
}
]
}
]
I was going through wiremock documentation and found below for math work, but didn't worked.
{{math 1 '+' 2}}
I have template for datafactory pipeline with destination dataset sink, which I want deploy using resource manager and Java Azure SDK:
{
"name": "[concat(parameters('factoryName'), '/', parameters('pipeline_pipelineConfiguration_pipelineTemplate_destinationDataset01'))]",
"type": "Microsoft.DataFactory/factories/datasets",
"apiVersion": "2018-06-01",
"properties": {
"linkedServiceName": {
"referenceName": "[parameters('pipeline_pipelineConfiguration_pipelineTemplate_destinationLinkedService01')]",
"type": "LinkedServiceReference"
},
"annotations": [],
"type": "DelimitedText",
"typeProperties": {
"location": {
"type": "AzureBlobStorageLocation",
"fileName": {
"value": "[concat('#concat(utcnow(\'yyyy-MM-dd\'),\'-',parameters('pipeline_pipelineConfiguration_destination'),',.txt\'')]",
"type": "Expression"
},
"container": ""
},
"columnDelimiter": ",",
"escapeChar": "\\",
"firstRowAsHeader": true,
"quoteChar": "\""
},
"schema": []
},
"dependsOn": [
"[concat(variables('factoryId'), '/linkedServices/', parameters('pipeline_pipelineConfiguration_pipelineTemplate_DestinationLinkedService01'))]"
]
}
I get exception:
com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape ''' (code 39)
most probably because of value parameter for fileName.
What would the best way to provide file name with date calculated on time of exporting data and part of the name taken from parameter ?
Use variable and make calculation of value this variable
or use replace() function.
I have a json file as below which I am getting as a response from rest API:
{
"label": " MARA LEYZIN",
"ClassCode": "PROFESSIONAL",
"actvFlg": "A",
"name": "MARA LEYZIN",
"Typ": {
"label": "C_TYP_LU",
"TypCode": "PROFESSIONAL "
},
"Address": {
"link": [],
"firstRecord": 1,
"pageSize": 10,
"searchToken": "multi",
"item": [
{
"label": "Address",
"addrTypFk": {
"label": "C_ADDRESS_TYPE_LU",
"addrTypCd": "INDUSTRY",
"addrTypDesc": "Industry"
}
}
]
}
I am trying to parse this in Java and to remove some unwanted json objects. Like I want the following string to be replaced by blank:
"link": [],
"firstRecord": 1,
"pageSize": 10,
"searchToken": "multi",
"item":
To achieve this I am trying the following approach:
String jsonStr = new String(Files.readAllBytes(Paths.get(inputFile)));
System.out.println(jsonStr);
jsonStr.replaceAll("link", "");
But it is not replacing the required string with blanks. Please help me in this.
string object is immutable , so basically if do you want to replace something
System.out.println(jsonStr.replaceAll("link", "")); this will print the replaced string but it will not affect the original string, however if you do this
jsonStr=jsonStr.replaceAll("link", "");
System.out.println(jsonStr); this will print the replaced string
First of all:
Your JSON is not validate. You're missing a closing curly bracket at the end of it.
{
"label": " MARA LEYZIN",
"ClassCode": "PROFESSIONAL",
"actvFlg": "A",
"name": "MARA LEYZIN",
"Typ": {
"label": "C_TYP_LU",
"TypCode": "PROFESSIONAL "
},
"Address": {
"link": [],
"firstRecord": 1,
"pageSize": 10,
"searchToken": "multi",
"item": [{
"label": "Address",
"addrTypFk": {
"label": "C_ADDRESS_TYPE_LU",
"addrTypCd": "INDUSTRY",
"addrTypDesc": "Industry"
}
}]
}
}
Second of all you should just change order of your commands to this:
jsonStr.replaceAll("link", "");
System.out.println(jsonStr);
Important addition:
And I would suggest you to use org.json library or even better JACKSON to parse JSON files.
Here's tutorial how to use jackson and it's my warmest suggestion.
You will save a lot of time and you can do whatever you like.
I have json file which i down from internet and saved to my app. Then i read this file and create json object . But i am not able to create json object .
This is the exception I am getting
org.json.JSONException: Expected literal value at character 3 of { \"resources\": { ..........
Below is my code to read input stream and create json object
private JSONObject readFileFromInpputStream(InputStream inst) throws JSONException
{
// TODO Auto-generated method stub
StringBuilder responseStrBuilder=null;
try {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inst, "UTF-8"));
responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null){
responseStrBuilder.append(inputStr);
}
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = responseStrBuilder.toString();
String k=str.replace("\"", "\\\"");
// String m =k.replaceAll("\t", "");
// String s= m.replaceAll("\n", "");
//String p = s.replaceAll("\\s", "");
Log.i(loggerName, loggerName + " str " +str);
//Log.i(loggerName, loggerName + " k " +p);
JSONObject obj = new JSONObject(k);
return obj;
}
Below is output string
{
"resources": {
"-xmlns:xliff": "urn:oasis:names:tc:xliff:document:1.2",
"string": [
{
"name": "sample1",
"value": "To <xliff:g id=\"receiver_name\"> Europe </xliff:g>"
},
{
"name": "cdata",
"value": "<![CDATA[<p>Text<p>]]>"
},
{
"name": "content_description_sample",
"value": " <b>Something</b>"
},
{
"name": "countdown",
"value": " <xliff:g id="time" example="5days">%1$s</xliff:g> until holiday"
},
{
"name": "picker_combined_view_fmt",
"value": " Combined view (<xliff:g id="count">%s</xliff:g>)"
},
{
"name": "configure_email_text",
"value": "No corporate email accounts have been configured on this device. To configure them, click <b>Here</b> "
},
{
"name": "invalid_credentials",
"value": "Authentication failed. Enter valid credentials."
},
{
"name": "link",
"value": "<b>Hello World</b> This is a test of the URL Example"
},
{
"name": "bold",
"value": "<b>This text is bold</b>"
},
{
"name": "emphasis",
"value": "<em>This text is emphasized</em>"
},
{
"name": "check_availability_button",
"value": "Check availability How are you"
}
],
"string-array": [
{
"name": "Array1",
"item": [
"en_US",
"en_GB"
]
},
{
"name": "Array2",
"item": [
"en_US",
"en_GB"
]
}
]
}
}{
\"resources\": {
\"-xmlns: xliff\": \"urn: oasis: names: tc: xliff: document: 1.2\",
\"string\": [
{
\"name\": \"sample1\",
\"value\": \"To<xliff: gid=\\"receiver_name\\">Europe</xliff: g>\"
},
{
\"name\": \"cdata\",
\"value\": \"<![
CDATA[
<p>Text<p>
]
]>\"
},
{
\"name\": \"content_description_sample\",
\"value\": \"<b>Something</b>\"
},
{
\"name\": \"countdown\",
\"value\": \"<xliff: gid=\"time\"example=\"5days\">%1$s</xliff: g>untilholiday\"
},
{
\"name\": \"picker_combined_view_fmt\",
\"value\": \"Combinedview(<xliff: gid=\"count\">%s</xliff: g>)\"
},
{
\"name\": \"configure_email_text\",
\"value\": \"Nocorporateemailaccountshavebeenconfiguredonthisdevice.Toconfigurethem,
click<b>Here</b>\"
},
{
\"name\": \"invalid_credentials\",
\"value\": \"Authenticationfailed.Entervalidcredentials.\"
},
{
\"name\": \"link\",
\"value\": \"<b>HelloWorld</b>ThisisatestoftheURLExample\"
},
{
\"name\": \"bold\",
\"value\": \"<b>Thistextisbold</b>\"
},
{
\"name\": \"emphasis\",
\"value\": \"<em>Thistextisemphasized</em>\"
},
{
\"name\": \"check_availability_button\",
\"value\": \"CheckavailabilityHowareyou\"
}
],
\"string-array\": [
{
\"name\": \"Array1\",
\"item\": [
\"en_US\",
\"en_GB\"
]
},
{
\"name\": \"Array2\",
\"item\": [
\"en_US\",
\"en_GB\"
]
}
]
}
}
and below is the exception i am getting
org.json.JSONException: Expected literal value at character 3 of { \"resources\": { ..........
What am I doing wrong?
There is no reason to escape quotation marks("). They are part of how the json object constructor identifies strings.
Just using
JSONObject obj = new JSONObject(str);
should be fine.
In addition,
in " Combined view (%s)" the two quotation marks are treated as string delimiters and SHOULD be escaped, but it indicates a problem with the server you got this message from. Escaping these yourself can be impossible because there is no sure way to know which quotation marks are real and which are part of the text.
On validating your JSON output through Jlint it gives validation error, but on removing space on line 2 of your output(is the space intentional ? or you added it on posting question by mistake?)
"cdata", "value": "Text
]]>" }, { "nam
it validates successfully.
In any case whether the space is initial source of error or not ,like CurlyCorvus said, simply pass the String to new JSONObject(str); without escaping the ".
Thans Mustafa and Curly.
The issue was due to quotes mark in tag
for ex in original one value is
"value": " %1$s until holiday"
It worked fine when I replaced it to
"value": " %1$s until holiday"
So I guess when quotes is present inside it consider it as new object.
I have the following result obtained from a custom result.
{
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}& ={count?}& start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&nsc={nsc?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
},
"queries": {
"nextPage": [
{
"title": "Google Custom Search - flowers",
"totalResults": 10300000,
"searchTerms": "flowers",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "013036536707430787589:_pqjad5hr1a"
}
],
"request": [
{
"title": "Google Custom Search - flowers",
"totalResults": 10300000,
"searchTerms": "flowers",
"count": 10,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "013036536707430787589:_pqjad5hr1a"
}
]
},
"context": {
"title": "Custom Search"
},
"items": [
{
"kind": "customsearch#result",
"title": "Flower - Wikipedia, the free encyclopedia",
"htmlTitle": "<b>Flower</b> - Wikipedia, the free encyclopedia",
"link": "http://en.wikipedia.org/wiki/Flower",
"displayLink": "en.wikipedia.org",
"snippet": "A flower, sometimes known as a bloom or blossom, is the reproductive structure found in flowering plants (plants of the division Magnoliophyta, ...",
"htmlSnippet": "A <b>flower</b>, sometimes known as a bloom or blossom, is the reproductive structure <br> found in flowering plants (plants of the division Magnoliophyta, <b>... </b>",
"pagemap": {
"RTO": [
{
"format": "image",
"group_impression_tag": "prbx_kr_rto_term_enc",
"Opt::max_rank_top": "0",
"Opt::threshold_override": "3",
"Opt::disallow_same_domain": "1",
"Output::title": "<b>Flower</b>",
"Output::want_title_on_right": "true",
"Output::num_lines1": "3",
"Output::text1": "꽃은 식물 에서 씨 를 만들어 번식 기능을 수행하는 생식 기관 을 말한다. 꽃을 형태학적으로 관찰하여 최초로 총괄한 사람은 식물계를 24강으로 분류한 린네 였다. 그 후 꽃은 식물분류학상중요한 기준이 되었다.",
"Output::gray1b": "- 위키백과",
"Output::no_clip1b": "true",
"UrlOutput::url2": "http://en.wikipedia.org/wiki/Flower",
"Output::link2": "위키백과 (영문)",
"Output::text2b": " ",
"UrlOutput::url2c": "http://ko.wikipedia.org/wiki/꽃",
"Output::link2c": "위키백과",
"result_group_header": "백과사전",
"Output::image_url": "http://www.gstatic.com/richsnippets/b/fcb6ee50e488743f.jpg",
"image_size": "80x80",
"Output::inline_image_width": "80",
"Output::inline_image_height": "80",
"Output::image_border": "1"
}
]
}
}
]
}
How can I extract all the https links from the above code using java?
You could be lazy and ignore parsing JSON, treat the entire result as a String and just use a regular expression to match URLs.
String httpLinkPattern = "https?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
Pattern p = Pattern.compile(httpLinkPattern);
Matcher m = p.matcher(jsonResult);
while (m.find())
System.out.println("Found http link: "+m.group());
If you are looking to convert your response to string for manipulation, and hence extract the URL's as oppose to using JSON library, then below should do.
public List<String> extractUrls(String input)
{
List<String> result = new ArrayList<String>();
Pattern pattern =
Pattern.compile("\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + "(\\w+:\\w+#)?(([-\\w]+\\.)+(com|org|net|gov"
+ "|mil|biz|info|mobi|name|aero|jobs|museum" + "|travel|[a-z]{2}))(:[\\d]{1,5})?"
+ "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" + "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?"
+ "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" + "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*"
+ "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
Matcher matcher = pattern.matcher(input);
while (matcher.find())
{
result.add(matcher.group());
}
return result;
}
Usage:
List<String> links = extractUrls(jsonResponseString);
for (String link : links)
{
System.out.println(link);
}
Please use JSON Parser to do this. I think that would be the best. Please refer the below link for nice example
Java code to parse JSON
https?://.*?\.(org|com|net|gov)/.*?(?=")
This regex will work for your purposes. http://regexr.com?30nm2