Binding nested JSON data in SAPUI5 XML view - json

I am trying to bind data from a nested JSON file in my SAPUI5 application. The view is in the XML format.
Here is the snippet from my JSON file:
{
"Departments": [
{
"ID": "1",
"Name": "Транспортный цех 1",
"Count": 35,
"Address": "Корпус 1, Этаж 7",
"Logo": "image/manager1.jpg",
"Employees": [
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
},
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
}
]
},
{
"ID": "2",
"Name": "Транспортный цех 2",
"Count": 35,
"Address": "Корпус 1, Этаж 7",
"Logo": "image/manager1.jpg",
"Employees": [
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
},
{
"ID": "1000001234",
"LastName": "Базенков",
"FirstName": "Андрей",
"MiddleName": "Анатольевич"
}
]
}
]
}
I am loading the JSON file in my controller and then binding the data "Address" and "Name" in my XML view as follows:
<List id="list1" items="{path:'/Departments'}">
<items>
<ObjectListItem icon="{Logo}" type="Active" press="onListItemPress" number="{Count}" title="{Name}">
<attributes>
<ObjectAttribute text="{Address}" />
</attributes>
</ObjectListItem>
</items>
</List>
However when I tried binding the nested data "FirstName" or "LastName" like this I am not able to bind it.
text="{Employees/LastName}"

Employees is an array.
You can pick one entry and use {Employees/0/LastName} if you like.
You can also use a formatter function to merge the employees to a string:
View:
<ObjectAttribute text="{path: 'Employees', formatter: '.formatEmployees'}"/>
Controller:
formatEmployees: function(aEmployees){
return aEmployees.map(function(employee){ return employee.LastName + ", " + employee.FirstName; }).join("; ");
}
You can use a list-control like sap.m.ListBox or sap.m.Tokenizer and bind the items to the Employees array.

Related

How do I pass dropdown values via JSON record response to react UI?

I need to get feedback on the preferred method of architecting a JSON response to not only provide data elements but also control the UI elements as well (specifically select boxes)
I am currently designing a suite of REST services to support a new application. All data will be served from the REST services and the frontend will be Spring-Boot/REACT.js.
We want all business logic to be managed at the data layer (Spring-Boot MicroServices). So all of the "select options" (dropdowns) will be driven by data provided by the Java Service via the RESTful services.
I am just looking for feedback on either of these options (or if you have a more preferred option, please feel free.
[
{
"id": 1,
"first_name": "Curry",
"last_name": "Aburrow",
"email": "caburrow0#gnu.org",
"gender": "Male",
"gender_options": {
"0":"Male",
"1":"Female"
}
"department": "Sales"
"department_options": {
"0":"Sales",
"1":"Legal",
"2":"HR",
"3":"Finance",
"4":"Operations"
}
},
{
"id": 2,
"first_name": "Joachim",
"last_name": "Lotte",
"email": "jlotte1#webnode.com",
"gender": "Male",
"gender_options": {
"0":"Male",
"1":"Female"
}
"department": "Finance"
"department_options": {
"0":"Sales",
"1":"Legal",
"2":"HR",
"3":"Finance",
"4":"Operations"
}
}
]
OR THIS
[
{
"id": 1,
"first_name": "Curry",
"last_name": "Aburrow",
"email": "caburrow0#gnu.org",
"gender": {
"selected":"Male",
"options": {
"0":"Male",
"1":"Female"
}
"department": {
"selected":"Sales",
"options": {
"0":"Sales",
"1":"Legal",
"2":"HR",
"3":"Finance",
"4":"Operations"
}
},
{
"id": 2,
"first_name": "Joachim",
"last_name": "Lotte",
"email": "jlotte1#webnode.com",
"gender": {
"selected":"Female",
"options": {
"0":"Male",
"1":"Female"
}
"department": {
"selected":"Finance",
"options": {
"0":"Sales",
"1":"Legal",
"2":"HR",
"3":"Finance",
"4":"Operations"
}
}
]
Option 1 is basic json providing the select option array in it's own element.
Option 2 is nested array.
Neither of which is overly complex, just looking for the more performant method.
I feel that department options and gender options should not be a part of the Object in the first place. Those should come from a separate json which can be either static or is configurable by some CMS. So you should just be fine with this:
[{
"id": 1,
"first_name": "Curry",
"last_name": "Aburrow",
"email": "caburrow0#gnu.org",
"gender": "Male",
"department": "Sales"
},
{
"id": 2,
"first_name": "Joachim",
"last_name": "Lotte",
"email": "jlotte1#webnode.com",
"gender": "Male",
"department": "Finance"
}
]
Also, your gender options and department options aren't really arrays. They're Objects. You can either make them string arrays or better, make them object arrays so that they could also have something like a display and value field that could then be used. For eg:
For gender options:
[{
display: "Male",
value: "MALE"
},
{
display: "Female",
value: "FEMALE"
}
]
For department options:
[{
id: 1,
display: "Sales",
value: "SALES"
},
{
id: 2,
display: "Legal",
value: "LEGAL"
},
{
id: 3,
display: "HR",
value: "HR"
},
{
id: 4,
display: "Finance",
value: "FINANCE"
},
{
id: 5,
display: "Operations",
value: "OPERATIONS"
}
]

I need to pass Json via Controller To View. HOW?

I have This Task i have been giving to do .All I need To do is Pass Json file (Meaning Reading it from storage) By the controller and then return it to the view and display it .That's All.
My Controller Function
public function ReadFromStorage(){
$path = Storage::get(storage_path('widget.json'));
return view('read', compact(path));
}
My Route
Route::get('/read','ReadJsonController#ReadFromPublic');
I believe this will work:
My Controller Function
public function ReadFromPublic(){
$path = Storage::get('widget.json');
return view('read', compact('path'));
}
My Route
Route::get('/read','ReadJsonController#ReadFromPublic');
And if your'e using the public disk(used by default), don't forget to run php artisan storage:link
You can read more here: https://laravel.com/docs/5.7/filesystem
// controller
public function ReadFromPublic(){
$path = public_path('clients.json');
$data = json_decode(file_get_contents($path));
$clients = $data->clients;
return view('read', compact('clients'));
}
// view file
#foreach($clients as $client)
<div>
{{$client->name}}
</div>
#endforeach
// sample json file
{
"clients": [
{
"id": "59761c23b30d971669fb42ff",
"isActive": true,
"age": 36,
"name": "Dunlap Hubbard",
"gender": "male",
"company": "CEDWARD",
"email": "dunlaphubbard#cedward.com",
"phone": "+1 (890) 543-2508",
"address": "169 Rutledge Street, Konterra, Northern Mariana Islands, 8551"
},
{
"id": "59761c233d8d0f92a6b0570d",
"isActive": true,
"age": 24,
"name": "Kirsten Sellers",
"gender": "female",
"company": "EMERGENT",
"email": "kirstensellers#emergent.com",
"phone": "+1 (831) 564-2190",
"address": "886 Gallatin Place, Fannett, Arkansas, 4656"
},
{
"id": "59761c23fcb6254b1a06dad5",
"isActive": true,
"age": 30,
"name": "Acosta Robbins",
"gender": "male",
"company": "ORGANICA",
"email": "acostarobbins#organica.com",
"phone": "+1 (882) 441-3367",
"address": "697 Linden Boulevard, Sattley, Idaho, 1035"
}
]
}

AttributeError: 'str' object has no attribute 'items' error

I am writing a python script for extracting information from the json file. I am printing the title of the book with lastname as Marcus. I have the output but it has the error AttributeError: 'str' object has no attribute 'items' error as well
import json
from pprint import pprint
with open('bibliography.json.txt', encoding='utf-8') as data_file:
data = json.load(data_file)
for entry in data['bibliography']['biblioentry']:
for authors in entry['author']:
for key,val in authors.items():
if(key== 'lastname' and val=='Marcus'):
title=entry['title']
print(title)
the json file looks like this:
{
"bibliography": {
"biblioentry": [
{
"-type": "Journal Article",
"title": "A brief survey of web data extraction tools",
"author": [
{
"firstname": "Alberto",
"middlename": "HF",
"lastname": "Laender"
},
{
"firstname": "Berthier",
"middlename": "A",
"lastname": "Ribeiro-Neto"
},
{
"firstname": "Altigran",
"middlename": "S",
"lastname": "da Silva"
},
{
"firstname": "Juliana",
"middlename": "S",
"lastname": "Teixeira"
}
],
"details": {
"journalname": "ACM Sigmod Record",
"volume": "31",
"number": "2",
"pages": "84-93"
},
"year": "2002",
"publisher": "ACM"
},......
I think it is because it interprets the json file as a string. I think you might want to see this if it helps you:
Extract data from JSON API using Python

How to combine columns in spark as a JSON in Scala

I have a variable which is constructed as follows extracting data using Spark SQL:
{
"resourceType" : "Test1",
"count" : 10,
"entry": [{
"id": "112",
"gender": "female",
"birthDate": 1213999
}, {
"id": "urn:uuid:002e27cf-3cae-4393-89c5-1b78050d9428",
"resourceType": "Encounter"
}]
}
I want the output in the following format:
{
"resourceType" : "Test1",
"count" : 10,
"entry" :[
"resource" :{
"id": "112",
"gender": "female",
"birthDate": 1213999
},
"resource" :{
"id": "urn:uuid:002e27cf-3cae-4393-89c5-1b78050d9428",
"resourceType": "Encounter"
}]
}
I am basically new to Scala :), would need help in this.
EDIT: Adding the scala code to create the JSON:
val bundle = endresult.groupBy("id").agg(count("*") as "total",collect_list("resource") as "entry").
withColumn("resourceType", lit("Bundle")).
drop("id").
select(to_json(struct("resourceType","entry"))).
map(row => row.getString(0).
replace("\"entry\":[\"{", "\"entry\":[{").
replace("}\"]}","}]}"). // Should be at the end of the string ONLY (we might switch to regex instead
replace("}\",\"{","},{")
replace("\\\"", "\"")
)

Liferay API update Data with JSON

I am working on an API for my Portal, to provide users the ability to update there data via API directly from there internal Systems.
Liferay 6.2 bundle Tomcat. (for api call testing I use soapUI)
The get Methods work fine, (I have getAll, and getByID). getByID returns a JSON Object like this:
{
"ID": "ORGANIZATIONID",
"type": "ORGANIZATIONTYPE",
"name": "ORGANIZATIONNAME",
"URI": "ORGANIZATIONNAMEURI"
"date of inclusion": "INCLUTIONDATE",
"last activities": "LASTMODIFIEDDATE",
"address": {
"name of host institution": "NAMEOFHOSTINSTITUTE",
"street1": "STREET1",
"street2" : "STREET2",
"zip": "ZIP",
"city": "CITY",
"country": "COUNTRY",
},
"url": [{"ORGANIZATIONURL"}],
"main contact": {
"first name": "FIRSTNAME",
"last name" : "LASTNAME",
"phone": "PHONE",
"email": "MAINCONTACTEMAIL"
},
"type of host institution" : "TYPEOFHOSTINSTITUTE",
"diseases": [
{
"name": "DISEASENAME1",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
},
{
"name": "DISEASENAME2",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
}
]
}
I would like to have an API for Updating the diseases information for an organization. I have created a URL service where everything is in the url as parameters, but I would like to have it that the in the url only the id parameter is send and the rest of the information in a JSON object. Like:
/api/jsonws/....-portlet...../updateregbb/organization-id/16016/
and the data:
{
"ID": "ORGANIZATIONID",
"diseases": [
{
"name": "DISEASENAME1",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
},
{
"name": "DISEASENAME2",
"number": "SAMPLECOUNT",
"gene": "GENE",
"orphacode": "ORPHA"
"icd10": "ICD",
"omim": "OMIM";
"synonym": "SYNONYM"
}
]
}
But I could not find a solution how I can read the JSON data:
#JSONWebService(value = "updateregbb", method = "POST")
public void updateregbb2(long organizationId, Map jsondata) {
#JSONWebService(value = "updateregbb", method = "POST")
public void updateregbb(long organizationId, JSONObject json) {
How can I get the Data into my function?
Thanks for the help,
Best
Robert