Get certain properties from large json object - angularjs

Im receiving a huge json object, I need to take only two properties from it.
The json comes like this
"{\r\n\"d\" : [\r\n{\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1191)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1191, \"PortName\": \"PORTLAND\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1192)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1192, \"PortName\": \"BANGOR\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1193)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1193, \"PortName\": \"LUBEC\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\":....
I need only the PortName and the PortID to be like this
0:{portCode:"",portName:""},
1:{portCode:"",portName:""},
...

var jsonString = "{\r\n\"d\" : [\r\n{\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1191)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1191, \"PortName\": \"PORTLAND\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1192)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1192, \"PortName\": \"BANGOR\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1193)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1193, \"PortName\": \"LUBEC\"\r\n}]}"
var jsonArray = JSON.parse(jsonString).d;
var arrayOfObjectsWithOnlyThose2Properties = jsonArray.map(function(item){
return {
"portCode": item.PortCode,
"portName": item.PortName
}
});
console.log(arrayOfObjectsWithOnlyThose2Properties);

Related

Spring boot - I want to write a POST endpoint to consume multipart/form-data WITHOUT any file uplload, I need to post json data as key-value pair pair

I want to do something like this -
#ApiOperation("Solve for tasks in JSON file")
#PostMapping(value = "/tasks/file",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<Plan> solveTest(#RequestBody(value = "file") InputStream filrInputStream) {}
I tried adding jersey multipart dependency in my spring boot app and tried my api method signature as below, but when I try posting my json string I get input stream as empty-
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
#ApiImplicitParams({
#ApiImplicitParam(
name = "file",
dataType = "java.io.InputStream",
examples = #io.swagger.annotations.Example(
value = {
#ExampleProperty(value = "[\r\n"
+ " {\r\n"
+ " \"duration\": 0,\r\n"
+ " \"xxxxxxTG\": \"string\",\r\n"
+ " \"sequence\": 0,\r\n"
+ " \"xxxxxx\": \"string\",\r\n"
+ " \"taskId\": \"string\",\r\n"
+ " \"taskType\": \"string\",\r\n"
+ " \"xcoordinate\": 0,\r\n"
+ " \"ycoordinate\": 0\r\n"
+ " }\r\n"
+ "]", mediaType = "multipart/form-data")
}))
})
#PostMapping(value = "/tasks/file",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE}
)
public ResponseEntity<Plan> solveTest1(#FormDataParam("file") InputStream file, #FormDataParam("file") FormDataContentDisposition fileMetaData)

Unable to register Flink TableSource with external connector

I am trying to register flink source with the below code snippet. But failed with the exception.
Exception in thread "main" org.apache.flink.table.api.TableException: findAndCreateTableSource failed.
at org.apache.flink.table.factories.TableFactoryUtil.findAndCreateTableSource(TableFactoryUtil.java:67)
at org.apache.flink.table.factories.TableFactoryUtil.findAndCreateTableSource(TableFactoryUtil.java:54)
at org.apache.flink.table.descriptors.ConnectTableDescriptor.registerTableSource(ConnectTableDescriptor.java:69)
at test.flink.BatchJob.main(BatchJob.java:58)
Caused by: org.apache.flink.table.api.NoMatchingTableFactoryException: Could not find a suitable table factory for 'org.apache.flink.table.factories.TableSourceFactory' in
the classpath.
Reason: No context matches.
The following properties are requested:
connector.path=file://D:\\Work\\flink\\flink-quickstart\\table-api\\src\\main\\resources\\result
connector.property-version=1
connector.type=filesystem
format.avro-schema={\"namespace\": \"example.avro\",\n \"type\": \"record\",\n \"name\": \"User\",\n \"fields\": [\n {\"name\": \"name\", \"type\": \"string\"},\n {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]},\n {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n ]\n}
format.property-version=1
format.type=avro
schema.0.name=name
schema.0.type=VARCHAR
schema.1.name=favorite_number
schema.1.type=INT
schema.2.name=favorite_color
schema.2.type=VARCHAR
The following factories have been considered:
org.apache.flink.table.sources.CsvBatchTableSourceFactory
org.apache.flink.table.sources.CsvAppendTableSourceFactory
org.apache.flink.table.sinks.CsvBatchTableSinkFactory
org.apache.flink.table.sinks.CsvAppendTableSinkFactory
org.apache.flink.table.catalog.GenericInMemoryCatalogFactory
org.apache.flink.table.planner.StreamPlannerFactory
org.apache.flink.table.executor.StreamExecutorFactory
org.apache.flink.formats.avro.AvroRowFormatFactory
at org.apache.flink.table.factories.TableFactoryService.filterByContext(TableFactoryService.java:283)
at org.apache.flink.table.factories.TableFactoryService.filter(TableFactoryService.java:191)
at org.apache.flink.table.factories.TableFactoryService.findSingleInternal(TableFactoryService.java:144)
at org.apache.flink.table.factories.TableFactoryService.find(TableFactoryService.java:97)
at org.apache.flink.table.factories.TableFactoryUtil.findAndCreateTableSource(TableFactoryUtil.java:64)
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
BatchTableEnvironment tEnv = BatchTableEnvironment.create(env);
String schemaString = "{\"namespace\": \"example.avro\",\n" +
" \"type\": \"record\",\n" +
" \"name\": \"User\",\n" +
" \"fields\": [\n" +
" {\"name\": \"name\", \"type\": \"string\"},\n" +
" {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]},\n" +
" {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n" +
" ]\n" +
"}";
tEnv
.connect(
new FileSystem()
.path("file://D:\\Work\\flink\\flink-quickstart\\table-api\\src\\main\\resources\\result")
)
.withFormat(new Avro().avroSchema(schemaString))
.withSchema(
new Schema()
.field("name", Types.STRING)
.field("favorite_number", Types.INT)
.field("favorite_color", Types.STRING)
)
.registerTableSource("FirstTableSource");
env.execute();
}```
The documentation seems to indicate that the filesystem connector only supports CSV.

Iterating through nested arrays with hyphen in object name

First of all, I know naming array objects with hyphens is completely incorrect, and I'm not the creator of this. I need to call an API within a service, and many objects are named improperly, like {"children-education": [ and { "Kid Stories": [.
I have tried assigning the name to a variable like let edChild = "child-education"and then parsing it to an object, like edChild = JSON.parse(edChild)to no avail. I really have no idea of what I'm doing, nor even if it's possible to do so.
I kinda have the option to call my customer and kindly ask his team to rename the objects to something less... stupid than special characters than I can't possible call in Typescript, but I'd like to learn if there's a way to surpass this in future occasions or if they can't rename it.
Here's an example of the JSON I'm trying to iterate through:
{
"business":
[
{
"anim":
[
{
"child-education": [
{
"Kid Stories": [
{
"id": 1,
"name": "Three Little Pinkies",
"url": "#",
"description": "shows how the world is beautiful"
},
(... and so on)
Thanks in advance.
You can parse that JSON string like any JSON string, using JSON.parse()
let str = "{\n" +
" \"business\":\n" +
" [\n" +
" {\n" +
" \"anim\":\n" +
" [\n" +
" {\n" +
" \"child-education\": [\n" +
" {\n" +
" \"Kid Stories\": [\n" +
"\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"Three Little Pinkies\",\n" +
" \"url\": \"#\",\n" +
" \"description\": \"shows how the world is beautiful\"\n" +
" }]}]}]}]}" +
""
console.log(JSON.parse(str));
After that, you can use bracket notation to access any property names
let str = "{\n" +
" \"business\":\n" +
" [\n" +
" {\n" +
" \"anim\":\n" +
" [\n" +
" {\n" +
" \"child-education\": [\n" +
" {\n" +
" \"Kid Stories\": [\n" +
"\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"Three Little Pinkies\",\n" +
" \"url\": \"#\",\n" +
" \"description\": \"shows how the world is beautiful\"\n" +
" }]}]}]}]}" +
""
const obj = JSON.parse(str);
console.log(obj.business[0].anim[0]['child-education'][0]['Kid Stories'][0]);

Can someone help me with an XPages DataView control for a mobile application?

I'm creating my first mobile interface for one of my xpage applications. My approach is fairly simple in that I'll use dataview controls to show view data and then moveto a document page from there.
My challenge is that I want to display my view data with more than just one value from a summary column. I want to show something like this:
Q12345 sent on 5/12/2018 v
ABC MANUFACTURING, CORP.
RT554039
RT223091
RT009873
where the first line contains the expandable details link on the right. The detail section would contain the "RT" numbers. So, by default each entry would display the Quote number, sent date and customer name. And then expanding the entry would show the "RT" numbers.
I found Brad Ballasaitis' blog article at
https://xcellerant.net/2013/08/02/xpages-data-views-3-collapsible-details/
but I can't get it work for me. This is what I see instead:
I also don't want the whole thing bold, just the quote number itself.
Here's my code:
<xe:appPage id="appPage10" resetContent="true"
pageName="viewByCust" preload="true">
<xe:djxmHeading id="djxmHeading10" back="Back"
moveTo="viewCustByRep">
<xe:this.label><![CDATA[#{javascript:sessionScope.AcctUNID = null;
"Quotes List"}]]></xe:this.label>
<xp:this.facets>
<xp:button value="Home" id="button1"
xp:key="actionFacet">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action>
<xe:moveTo direction="Right to Left"
forceFullRefresh="true" targetPage="homePage"
transitionType="slide">
</xe:moveTo>
</xp:this.action>
</xp:eventHandler>
</xp:button>
</xp:this.facets>
<xp:label id="label9" rendered="false"
style="color:rgb(255,0,0);margin-left:50.0px;font-weight:bold;font-size:11pt"
value="viewByCust">
</xp:label>
</xe:djxmHeading>
<xp:table style="width:100.0%">
<xp:tr>
<xp:td style="text-align:center;height:30px">
<xp:label id="label2"
style="font-size:12pt;font-weight:bold" value="#{javascript:sessionScope.ViewKey}">
</xp:label>
</xp:td>
</xp:tr>
</xp:table>
<xe:dataView id="dataView1" openDocAsReadonly="true"
rows="100" var="ventry" collapsibleDetail="true"
disableHideRow="false">
<xe:this.pageName><![CDATA[#{javascript:sessionScope.BackTo = "viewByCust";
"pageDoc"}]]></xe:this.pageName>
<xp:this.facets>
<xp:panel xp:key="summary" id="panelsumm">
<xp:text escape="false" id="computedField23">
<xp:this.value><![CDATA[#{javascript:if(ventry!=null){
var tmpstr = "<h4><a href='PageName.xsp?openDocument&documentId=" + ventry.getDocument().getUniversalID() + "'>";
tmpstr = tmpstr + ventry.getDocument().getItemValueString('DocNum') + "</a></h4><b></b> sent on asdfasdf<br></br>";
tmpstr = tmpstr + " " + ventry.getColumnValues()[4];
}}]]></xp:this.value>
</xp:text>
</xp:panel>
<xp:text escape="false" id="computedField24"
xp:key="detail">
<xp:this.value><![CDATA[#{javascript:"RT123<br></br>RT5543<br></br>RT9876"}]]></xp:this.value>
</xp:text>
<xp:link escape="true" text="More Entries..."
id="link1" xp:key="pagerBottom">
<xp:this.rendered><![CDATA[#{javascript:sessionScope.AcctSearch==null || sessionScope.AcctSearch==""}]]></xp:this.rendered>
<xp:eventHandler event="onclick"
submit="false">
<xp:this.script>
<xe:addRows rowCount="50"
for="dataView2">
</xe:addRows>
</xp:this.script>
</xp:eventHandler>
</xp:link>
<xe:toolBarButton id="toolBarButton3" label="Search"
xp:key="pagerTop">
<xp:eventHandler event="onClick" submit="true"
refreshMode="partial" refreshId="dataView1">
</xp:eventHandler>
</xe:toolBarButton>
<xp:inputText id="inputText4"
value="#{sessionScope.AcctSearch}" xp:key="pagerTopLeft"
style="height:30px;font-size:12pt">
<xp:eventHandler event="onkeydown" submit="true"
refreshMode="partial" refreshId="dataView2">
<xp:this.script>
<xp:executeClientScript>
<xp:this.script><![CDATA[if (thisEvent.keyCode != '13') {
return false;
}]]></xp:this.script>
</xp:executeClientScript>
</xp:this.script>
</xp:eventHandler>
</xp:inputText>
</xp:this.facets>
<xe:this.data>
<xp:dominoView var="view2"
viewName="SalesQbyCust">
<xp:this.categoryFilter><![CDATA[#{javascript:var vkey = sessionScope.ViewKey;
if(vkey==null){
sessionScope.RepNum;
}
else {
sessionScope.RepNum + "~" + vkey;
}}]]></xp:this.categoryFilter>
</xp:dominoView>
</xe:this.data>
</xe:dataView>
</xe:appPage>
Can someone help me with where I've gone wrong and point me in the right direction to get this working?
(Just to be clear, I'm hoping to have both a 'details expanding' drop down arrow AND the normal 'goto' right arrow for each entry.)
The piece you need to address is this one:
var tmpstr = "<h4><a href='PageName.xsp?openDocument&documentId=" + ventry.getDocument().getUniversalID() + "'>";
tmpstr = tmpstr + ventry.getDocument().getItemValueString('DocNum') + "</a></h4><b></b> sent on asdfasdf<br></br>";
tmpstr = tmpstr + " " + ventry.getColumnValues()[4];
You need to rework the HTML by adding class names and div tags with class entries. Also - for the sake of speed: Try not to mix columnValues and Items. Move all values you are interested in into the view, then you don't need to call the document. Or pick everything from the document (slower)
var colvals = ventry.getColumnValues();
var tmpstr = "<h4 class='mobileControl'><a href='PageName.xsp?openDocument&documentId=" + ventry.colvals[0] + "'>";
tmpstr = tmpstr + ventry.colvals[1]+ "</a></h4>"
tmpstr = tmpstr + "<div class='mobileDetail' sent on asdfasdf<br />";
tmpstr = tmpstr + colvals[4] + "</div>";
Then use CSS to format it
Thanks to both of you guys. I removed the doc reference (that was a hold over from my previous attempts).
I was able to get the look I was after with this computed html in the Summary facet:
if(ventry!=null){
var docnum = ventry.getColumnValues()[1];
var custname = ventry.getColumnValues()[4];
var ndt = ventry.getColumnValues()[5];
var itemlist = ventry.getColumnValues()[2]; //item~desc+item~desc
var itmdscarr = itemlist.split("+");
//just keep the desc (model #s)
var items = new Array();
for(x=0;x<itmdscarr.length;x++){
var tmp = itmdscarr[x]; //item~desc
var tmparr = tmp.split("~");
items.push(tmparr[1]);
}
var jdt = ndt.toJavaDate();
var tmpstr = "<font size='3'>";
tmpstr = tmpstr + docnum + "</font> " + jdt.toDateString() + "<br>";
tmpstr = tmpstr + " " + custname +"<br>";
tmpstr = tmpstr + "<font size='2' face='Courier'><UL style='list-style-type:none;margin-top:0px;margin-left:0px'><LI>" + #Implode(items,"<LI>") + "</UL></font>";
}
which gives me this...
I removed the summaryColumn name and simply put this computed field in a panel in the Summary facet. Works like a charm.
Thanks again.

How to use radio button in html and javascript to insert into the database in CN1

I was trying to get the values of radio button in html page when checked and insert it into the sqlite database and mysql. This is my full code below:
Form hi = new Form("Hi World");
final WebBrowser b = new WebBrowser(){
#Override
public void onLoad(String url) {
// Placed on onLoad because we need to wait for page
// to load to interact with it.
BrowserComponent c = (BrowserComponent)this.getInternal();
// Create a Javascript context for this BrowserComponent
JavascriptContext ctx = new JavascriptContext(c);
JSObject window = (JSObject)ctx.get("window");
window.set("addAsync", new JSFunction(){
public void apply(JSObject self, final Object[] args) {
String a = (String)args[0];
// Double b = (Double)args[1];
System.out.println("Value picked is "+a);
JSObject callback = (JSObject)args[1];
callback.call(new Object[]{new String(a)});
System.out.println("ok Good");
}
});
}
};
b.setPage( "<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" <script>\n" +
"function test() {"+
" var radios = document.getElementsByName('radiotest');"+
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
"}"+
// + \n" +
" var radios = document.getElementsByName('radiotest')"+
" document.addEventListener('click', function(){\n" +
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
" a = document.getElementById('val1').value ;\n" +
" b = document.getElementById('val2').value\n" +
" window.addAsync(a, b, function(result){\n" +
" document.getElementById('result').innerHTML = result;\n" +
" });\n" +
" }, true); "
+ "" +
" </script>\n" +
" </head>\n" +
" <body >\n" +
" <table border='0' cellspacing = '15'>"+
" <thead>"+
" </thead>"+
" <tbody>"+
" <tr>"+
" <td><font size = '3'> <b>A </font></b><input type='radio' name='radiotest' id='val1' onclick='test()' value='1' style='border: none;'> value1</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td><font size = '3'> <b> B</font></b><input type='radio' name='radiotest' id='val2' onclick='test()' value='2' style='border: none;'> value2</input>"+
"</td>"+ " </tr>"+
" <tr>"+
" <td> <font size = '3'> <b>C </font></b><input type='radio' name='radiotest' id='val3' onclick='test()' value='3'style='border: none;'> value3</input>"+
" </tr>"+
"<tr>"+
" <td> <font size = '3'> <b>D </font></b> <input type='radio' name='radiotest'id='val4' onclick='test()' value='4'style='border: none;'> value4</input>"+
" </tr>"+
"</tbody>"+
"</table>"+
"<span id=\"result\"></span>"+
" </body>\n" +
"</html>", null);
hi.setLayout(new BorderLayout());
hi.setLayout(new BorderLayout());
hi.addComponent(BorderLayout.CENTER, b);
hi.show();
The code above did not work for me. Pls help.
The problem am having now is to add actionListener to the radio button when checked by collecting its value and insert these values into the database accordingly.
I have edited my code, With this I can get the value selected with javascript function with the code below. Now, How can I insert the values of the radio button into the database as I click on radio button.
Form hi = new Form("BrowserComponent", new BorderLayout());
BrowserComponent bc = new BrowserComponent();
bc.setPage( "<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" </head>\n" +
" <body >\n" +
" <table border='0' cellspacing = '15'>"+
" <thead>"+
" </thead>"+
" <tbody>"+
" <tr>"+
" <td><font size = '3'> <b>A </font></b><input type='radio' name='radiotest' id = 'val1' onclick='test()' value='1' style='border: none;'> value1</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td><font size = '3'> <b> B</font></b><input type='radio' name='radiotest'id = 'val2' onclick='test()' value='2' style='border: none;'> val2</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td> <font size = '3'> <b>C </font></b><input type='radio' name='radiotest' onclick='test()' value='3'style='border: none;'> val3</input>"+
" </tr>"+
"<tr>"+
" <td> <font size = '3'> <b>D </font></b> <input type='radio' name='radiotest' onclick='test()' value='4'style='border: none;'> val4</input>"+
" </tr>"+
"</tbody>"+
"</table>"+
" <script type='text/javascript'>" +
//This javascript is perfect now.
"function test() {"+
" var radios = document.getElementsByName('radiotest');"+
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
// " instruction = document.getElementById('input3').value" +
// "'"+option_code2+"'"+
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
"}"+
"</script>"+
" </body>\n" +
"</html>", null);
TextField tf = new TextField();
hi.add(BorderLayout.CENTER, bc).
add(BorderLayout.SOUTH, tf);
//bc.addWebEventListener("onLoad", (e) -> bc.execute("fnc('<p>Hello World</p>')"));
hi.show();
Make sure your libraries are up to date, if the JavaScript bridge works correctly for setURL but fails for setPage it sounds like something we fixed in a recent update.

Resources