How to update nested data from a PUT request - json

Suppose we have a Invoice that is saved in a draft state in a RDBMS. The invoice has three line items. A rest client gets the invoice, and modifies the line items.
GET /invoice/123
{
"InvoiceId" : "123",
"lineitems":
[
{ "id":"A", "qty":"5"},
{ "id":"B", "qty":"5"},
]
}
Assume user modifies the invoice as follows;
Changes quantity of Item A
Removes Item B.
Adds new item C.
The result is:
{
"InvoiceId" : "123",
"lineitems":
[
{ "id":"A", "qty":"10"},
{ "id":"D", "qty":"5"},
]
}
The net change is that a line has been deleted, a line has been updated, and a line has been added.
User then saves the draft invoice, which results in a PUT request.
PUT /invoice/123
What is the best strategy to update the line items on the server?
Keep in mind that deleteing all line items and creating them from scratch will loose any meta data on those lines (Created by, Created date, last modified by...)
Thanks,

The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Think of PUT as replace. The state of the resource in the PUT request should be the resulting state of the resource if it is accepted.
If you want to add/remove/update specific elements you should look into using PATCH.

Related

How to retrieve and update JSON value In LogicApp using 'For Each'

Background:
I wish to loop through, retrieve and update items within my document
I wish to locate and loop through values within an array, locate nested values and then update them using LogicApps. I know I am on the right path, I just need a little assistance.
1.Here is the document. I wish to loop over A (it has many records in it) and retrieve all of the
categories.
"A":
[
{
"category": "1"
}
]
2.Once I retrieve the categories, I wish to loop over each one of the categories I find and then update their id and status
"category":"1"
"box":
[
{
"id": "update"
"status": "update"
}
]
My approach and what I have done:
I believe I have to do a 'For Each' loop over the A's.
I am just not sure how to write in the logicApp that I want to retrieve all the "category" fields and then the fields within the box field.
Then once I obtain all the "category" fields, I must update the id and status within this array with
a string.
I want to change
"id": "update" to "id" : "number"
and change
"status": "update" to "status": "linked"
I believe I must: initialize, parse, and do a For Loop
I believe I must: initialize, parse, and do a 'For Each' Loop, but how do I tell the logicapp I wish to grab the "category" part of the "A" since its nested, and then once I have the "category" part to then update the nested fields of "id" and "status" within that?
Any help is great
You have two options to do the editing.
First is the traditional Logic Apps way:
Use a set of working variables and the setProperty() function to update values as you go along. One variable to hold the portion you are currently editing and an array to hold the work done so far. Since you are working with nested arrays, you'll need two sets of variables- one set for the array inside "A" and one for the array inside "box". In order to update child properties, you just nest setProperty() calls: setProperty(<object>['<parent-property>'], '<parent-property>', setProperty(<object>['parentProperty'], '<child-property>', <value>)).
If you haven't already, you may want to take a look at this post, as it has a similar premise, though is a bit simpler in structure: Updating Json Array in LogicApp
Second option is to use the inline code action
In your case, I would probably go this route as the number of actions required drops significantly, making the app easier to read and reducing the cost. As long as your data is not too large, you could do all of your edits in a single action and simply output the result. If you want to keep some of the structure or you have a large object, you could keep elements of both- use the loop on your "A" array and then make your changes to that iteration inside Javascript.

Making a Tree like Structure in Json With Recursion

I'm struggling to get my head around this one, i've had a few attempts and failed miserably.
Basically I have a list of Employees in a database, each with an ID. A Employee can have a parent Employee (Refer as Manager).
I need to output the structure in some sort of tree in json format like shown in this URL
https://raw.githubusercontent.com/bumbeishvili/Assets/master/Projects/D3/Organization%20Chart/redesignedChartLongData.json
Here is a very basic structure example
The JSON can have a structure like this, containing an array of employees:
{
"employees":[
{
"id": 1764,
"name": "Maaz",
... //other fields... this employee has no parent field or
// "parent" : 0 (invalid value)
},
{
"id": 1765,
"parent": 1764,
//other fields... this employee has Maaz as parent
}
]
}
"parent" can simply be an optional field of the employee, containing theid of the parent employee.
If the json had a recursion (i.e. "parent": { /*...*/ }) this would create several issues such as:
Will the parent employee only be stored as second level, or also appear in the list as top level? This adds redundancy or alternatively, search complexity. because if 1764 only appears as child of 1765, you have to potentially recursively trasverse the whole list tree in order to find any employee.
What is the maximum depth of the recursion? With trasversal problems.
Inconsistency or circular relations, leading to loops. If A is parent of B and B is parent of A. Or if there is redundancy but upon deletion the employee does not get wiped anywhere.
More complex operations: if you have a chain of 5 parent-child employees and remove the top one, what do you do with the rest? You would have to define a policy. And adjust the tree everytime.
A flat list on the other hand required just 2 O(n) searches when you are looking for example, for employee 1765. You will then find its parent id and search the list again for an employee with than id, if it exists.
When you add an employee, add without a parent field and edit it later. When you delete an employee, just delete it and eventual broken links will be found at (flat) search time.

A field reference identifier must not have the form of a qualified name

I have a json file with the following structure:
{
"0.0.1":{
"version":"0.0.1",
"date_created":"2014-03-15"
},
"0.1.2":{
"version":"0.1.2",
"date_created":"2014-05-21"
}
}
As you can see the whole json file contains just one object which is a map in which each key is the version number and the value is a new map with version and date_created properties.
I want to use Apache Drill to get a list with two columns: version and date_created
But since the keys contain dots (e.g. "0.0.1") Drill throws the following error:
Error: SYSTEM ERROR: UnsupportedOperationException: Unhandled field reference "0.0.1"; a field reference identifier must not have the form of a qualified name (i.e., with ".").
... when running a query like this:
SELECT KVGEN(t.*) FROM dfs.`D:/drill/sample-data/myjsonfile.json` AS t;
By the way, how do you tell KVGEN to process the WHOLE ROW since the row object is the actual map we want to convert?
Any ideas about how to overcome this problem?
UPDATE:
Are all the map keys always different? yes they are. But I think that even in the case they were not, Drill should process them anyway and whenever it would find a duplicate, it should keep the last one (which normally is supposed to be the most recent one for most cases).
The main problem is that the JSON file contains ONLY one big object so there is only 1 record (row) and all its properties are the actual keys to each version data. So there are NO subproperties to navigate to when using KVGEN; the idea is that KVGEN processes the WHOLE record object and not a subproperty of it.
As someone has requested to get more sample data, here it is below:
{
"0.0.1":{
"version":"0.0.1",
"date_created":"2014-03-15"
},
"0.1.2":{
"version":"0.1.2",
"date_created":"2014-05-21"
},
"0.1.5":{
"version":"0.1.5",
"date_created":"2014-05-29"
},
"0.1.7":{
"version":"0.1.7",
"date_created":"2014-06-13"
},
"1.0.0":{
"version":"1.0.0",
"date_created":"2014-07-05"
},
"1.1.0":{
"version":"1.1.0",
"date_created":"2014-09-02"
}
}

Updating particular field in REST call jpa

Am new to REST webservice, i have a scenario where each field in a form to be save to the database as when user fills the field.
So am calling an update API when user goes from one field to another. Using jpa for database operation. The problem am facing here is, lets say for example Employee.
{
"fname":
"lname":
"mname":
"addresses":
{
"line1":
"line2":
}
}
While user fills fname my json will be
{
"fname":"some value"
}
ill call rest to update. And for next field
{
"lname":"another value"
}
in this update case lname will be updated but fname updated with a blank value since am not passing fname. So i have to send full json to have a proper value.
Think if my json is too big with many relation, am feeling just to update one field its not good to pass complete json to the REST API's.
Can somebody give me any idea or any solution for this.
Am developing this with Spring-boot framework
Thanks in advance.

jqGrid Access Extra Information

I have a dynamically created jqGrid with a custom formatter. I need to be able to access some information that is not in a grid column within the formatter. My problem is that the rowObject seems to contain only the information from the columns and nothing else, even though the JSON data has an extra attribute.
My JSON looks like:
{"rows":
[
{"cell":["637","Alice","Test","01\/01\/1980",""],"id":"637","patient_id":"637"},
...
{"cell":["635","Alice","Test","01\/01\/1980",""],"id":"635","patient_id":"635"},
],
"form_id":"3",
"records":"35",
"total":2,
"goto_patient_sidebar":"1",
"totalRecords":"35",
"dynamicFilters":"",
"listName":"Patients",
"page":1,
"recordsReturned":25,
"columns":
[
{"id":"75","field_id":"2","zmr_list_id":"13","alias":"Last Name",
"sortorder":"0","reference_field_id":"90","show_date":"0","show_time":"0"},
{"id":"76","field_id":"1","zmr_list_id":"13","alias":"First Name",
"sortorder":"1","reference_field_id":"90","show_date":"0","show_time":"0"},
{"id":"77","field_id":"25","zmr_list_id":"13","alias":"DOB",
"sortorder":"2","reference_field_id":"90","show_date":"1","show_time":"0"},
{"id":"78","field_id":"47","zmr_list_id":"13","alias":"Phone Number",
"sortorder":"3","reference_field_id":"90","show_date":"0","show_time":"0"}
]
}
For each row, I need to access the patient_id from the JSON. The data for the columns of each row is contained in the 'cell' for each row. The rowObject seems to contain only the information in each cell attribute of the JSON. How can I access patient_id without it being rendered in the column?
In the data the values of patient_id are always the same as the id. I suppose that there can be different (if patient_id are equal to id you can see the information in the custom formatter already). You can implement what you need in at least three ways:
You add an hidden column (having the property hidden:true) which will represent the patient_id. You move the information about the patient_id in the JSON input inside the "cell" array.
You place the information about the mapping between id and patient_id as the part of userdata which you will include in the JSON (see here for more information).
You use data parameter of the loadComplete event handle. The data parameter will contain full JSON data posted from the server. You can get the information which you need from the data and save it somewhere. Then you get the saved information from the custom formatter.
One more small remark about your JSON data. Currently you use items like
{
"cell": [
"637",
"Alice",
"Test",
"01\/01\/1980",
""
],
"id": "637",
}
(if we forget about the patient_id). It means that you send id information twice: one as the part of the column and second time for the first column of the grid. If you would include key:true setting in the definition of the first grid column and add jsonReader: {cell:""} you could make the JSON data in more compact where the row item would be in the array form
[
"637",
"Alice",
"Test",
"01\/01\/1980",
""
]
Add a hidden column for the extra data.