How can I add a key, value pair on a array of immutable objects? - immutable.js

I have an array of immutable objects and I want to add a key value pair on all the elements of the array. How can I do this?

Array.map(item => item.set(‘key’, value))

Related

Postgres searching with JSONB field for keys and/or values of json object

I am looking for find how to correctly query a Postgres JSONB field. Suppose I have a JSON object like so
{"key1": ["value1", "value2"], "key2": ["value1", "value3"]}
And I'm storing it in the field 'data', I can query for the existence of the key.
SELECT data from somethings WHERE data ? "key1"
Or the key and the value.
SELECT data from somethings WHERE data -> "key1" ? "value1"
But I am struggling to search by the existence of the key values. I'm looking for something like. Basically I want to find the existence of a value whether is a top-level key or one of the values in each array.
SELECT data from somethings WHERE data ? ".*" -> "value1"
I thought I was looking for jsonb_each for a bit but I am unsure how to leverage it. Any thoughts?
You can use a JSON path expression:
select *
from something
where data #? '$.* ? (#[*] == "value1")'
The $.* iterates over all keys, and the #[*] then iterates over all array elements for each key.

How to check if JSON response fields are alphabetically sorted?

How to validate the JSON Response fields? After validating the fields I need to check whether the fields are alphabetically sorted or not. How would I do it ?
The JSON object is unordered set of name/value pairs. There is no guarangee of ordering at all. You need to take json object keys list and sort them. After sorting access to object fields by sorted keys list.
If you mean how to check list (array) of values. You need to implement simple loop through array and check that each element must be less than next element in sorting comparision criteria.
For js language checking array for alpha ordering may be done like this:
var array = ["Apple", "Application", "AppName", "Happy"];
var isSortedAlpha = array.reduce(function(res, current, index, arr) {
return res && arr[index&&index-1] <= current
}, true);

scala json property value cleanup: single item array to item and property deletion for empty array

I have multiple json string lines. I loaded them as a DataFrame.
After merging data by Id, for an Id, all properties become arrays like:
{"id":123, "color":[A,B,C], "year":[1999], "nickname":[], "process":[[...]], ...}
I want to make this kind of json lines to:
{"id":123, "color":[A,B,C], "year":1999, "process":[...], ...} // deleting "nickname"
How can I do this?

Print JSON Object by key in JADE

I have a JSON object like this
{
"54634b4ea624675649e91bcd": "2014-12-04T09:21:02.622Z",
"542e7c720fa8e288631a8298": "2014-12-04T09:21:08.672Z",
"542c024f0fa8e288631a8285": "2014-12-19T15:28:27.092Z",
"542beaaf0fa8e288631a8270": "2014-12-19T15:29:22.546Z",
"542e82db0fa8e288631a8299": "2014-12-19T15:31:30.282Z",
"542e83870fa8e288631a829a": "2014-12-19T15:32:22.758Z",
"542e86e50fa8e288631a829c": "2014-12-19T15:33:07.509Z"
}
now i want to print the value of the key 542e7c720fa8e288631a8298 which is 2014-12-04T09:21:08.672Z
How can i print the value of second key in JADE
I have the key 542e7c720fa8e288631a8298 stored in another variable. Any help is much appreciated.
I dont know much about Jade but you may to iterate over the json obj array:
each value, key in obj
h=key
p=value
Then compare your variable(which has key) with 'key'.

Counting JSON nodes

How to calculate the how many objects in JSON object
my code is
String json = IOUtils.toString(is);
JSONObject jo = new JSONObject(json);
Here 'jo' is the jsonobeject with in that object some data is there that data is:
{
"cargoDetails":[{
"noOfPackages":"2",
"packType":"AUG",
"uom":"LB",
"packLength":"10",
"actualWt":"5",
"packHeight":"2",
"packWidth":"4",
"packDescription":"description",
"currId":"1",
"shipmentMode":"1"},
{"noOfPackages":"2",
"packType":"BAG",
"uom":"KG",
"packLength":"10",
"actualWt":"5",
"packHeight":"2",
"packWidth":"4",
"packDescription":"description",
"currId":"1",
"shipmentMode":"1"}],
"type":"air",
"userId":"KOTESWAR",
"customerId":"CUST00168",
"contractId":"CONTRACT0000576",
"originTerminalId":"FINBOM",
"destinationTerminalId":"FINDEL",
"ShipperId":"CUST00003",
"serviceLevelId":"STD",
"paymentTerms":"Prepaid",
"terminalId":"FINBOM",
"originDepartment":"AE",
"destinationDepartment":"AI",
"departurePort":"INBOM",
"destinationPort":"BOM",
"totalNoPcs":"4",
"totalActualWt":"5",
"totalVol":"10"
}
k with in that i can send cargodetails objects 2 remaining are bookingdetails
so my question is how to find the how many objects are there in jO.
because with that jo only i can iterate the loop.
If you are asking about how to define how many keys are in object, you should use Object.keys(json), which returns an array with all own keys. Yet, it is rather new function, not implemented in all browsers that are currently in use. Without Object.keys you have to use (for key in json) loop, testing each key with hasOwnProperty.
By the way, hasOwnProperty also can be not implemented in some browsers.