I have an array of hash as shown here. I want to merge the values of some fields with custom seprators. Here, i show only two hashes in the array, it is possible to have more. But, they are always in same sequence as shown here.
{
"details": [
{
"place": "abc",
"group": 3,
"year": 2006,
"id": 1304,
"street": "xyz 14",
"lf_number": "0118",
"code": 4433,
"name": "abc coorperation",
"group2": 3817,
"group1": 32,
"postal_code": "22926",
"status": 2
},
{
"place": "cbc",
"group": 2,
"year": 2007,
"id": 4983,
"street": "mnc 14",
"lf_number": "0145",
"code": 4433,
"name": "abc coorperation",
"group2": 3817,
"group1": 32,
"postalcode": "22926",
"status": 2
}
],
"#timestamp": "2017-09-04",
"parent": {
"child": [
{
"w_2": 0.5,
"w_1": 0.1,
"id": 14226,
"name": "air"
},
{
"w_2": null,
"w_1": 91,
"id": 25002,
"name": "Water"
}]
},
"p_name": "anacin",
"#version": "1",
"id": 28841
}
I want to edit the details. I want to construct new fields.
Field 1) coorperations: (details.name | details.postal_code details.street ; details.name | details.postal_code details.street)
Output:
Coorperations: (abc coorperation |22926 xyz 14; abc coorperation | 22926 mnc 14)
Field 2) access_code: (details.status-details.id-details.group1-details.group2-details.group(always two digit)/details.year(only last two digits); details.status-details.id-details.group1-details.group2-details.group(always two digit)/details.year(only last two digits))
Output: access_code (2-32-3817-03-06; 2-32-3817-02-07)
How can I achieve this for all the values in details. Here is how final results should look like.
{
"#timestamp": "2017-09-04",
"parent": {
"child": [
{
"w_2": 0.5,
"w_1": 0.1,
"id": 14226,
"name": "air"
},
{
"w_2": null,
"w_1": 91,
"id": 25002,
"name": "Water"
}]
},
"p_name": "anacin",
"#version": "1",
"id": 28841,
"Coorperations" : "abc coorperation |22926 xyz 14; abc coorperation | 22926 mnc 14",
"access_code" : "2-32-3817-03-06; 2-32-3817-02-07"
}
You can try to run this code in rails console with hash is your json:
new_hash = hash.except(:details)
coorperations = ""
access_code = ""
elements = hash[:details]
elements.each do |element|
coorperations = "#{coorperations}#{if coorperations.present? then '; ' else '' end}#{element[:name]} | #{element[:postal_code]} #{element[:street]}"
access_code = "#{access_code}#{if access_code.present? then '; ' else '' end}#{element[:status]}-#{element[:id]}-#{element[:group1]}-#{element[:group2]}-#{element[:group1]}-#{element[:group]}"
end
new_hash.merge!(Coorperations: coorperations)
new_hash.merge!(access_code: access_code)
new_hash
Related
I have an array called $customerRecords. I want to group the data in this array by the customer's email.
This is the array below
$customerRecords = [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
]
This is the expected result I want to achieve so that I can know the group of data that belongs to an email .
{
"doe#mailnator.com": [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
}
],
"john#mailnator.com": [
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
}
]
}
So this is what I have tried but it's not working:
return collect($customerRecords)->groupBy('customer.email')
you are almost done just define customer 0 item then email
return collect($customerRecords)->groupBy('customer.0.email');
This is how I was able to solve it.
$grouped = [];
foreach($customerRecords as $value) {
foreach($value['customer'] as $cust) {
$grouped[$cust['email']][] = $value;
}
}
I have some sample payload that I am going to be receiving, it looks like this:
[
{
"Id": "9",
"Line": [
{
"Amount": 100,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "1",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "TAX"
},
"UnitPrice": 25
}
},
{
"Amount": 100,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
},
{
"Id": "10",
"Line": [
{
"Amount": 140,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "1",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 35,
"Description": "Pest Control Services",
"DetailType": "SalesItemLineDetail",
"Id": "2",
"LineNum": 2,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Pest Control Services",
"value": "54"
},
"ItemRef": {
"name": "Pest Control",
"value": "10"
},
"Qty": 1,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 175,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
}
]
These I know are valid and I need to cross reference them, by id, in another payload I am receiving. But, the data I am receiving I can't assume to have valid ID's.
So, I want to take all the valid Ids from above, and shove them, randomly, into the sample data I have, that looks like this ($.invoices[].qbId):
[
{
"id": "fb2430c5-5970-46b0-9947-aaa0b9f177bb",
"invoices": [
{
"description": "2022-02-03 - 179",
"dueDate": "2022-02-03T22:51:10.206Z",
"id": "6f904b18-71c6-4fec-a016-7452f6a6b1dc",
"invoiceDate": "2022-02-03T22:51:10.347Z",
"openBalance": 200,
"paidAmount": 200,
"qbId": "1",
"totalAmount": 212
}
]
},
{
"id": "fa5b77b5-bfd4-4178-ac31-386ec83f530c",
"invoices": [
{
"description": "2022-01-12 - 95",
"dueDate": "2022-01-12T14:08:26.219Z",
"id": "05a58be3-4396-4c15-b9c2-ece68cb2b3fb",
"invoiceDate": "2022-01-12T14:08:26.399Z",
"openBalance": 7.33,
"paidAmount": 7.33,
"qbId": "",
"totalAmount": 7.33
},
{
"description": "2022-01-12 - 95",
"dueDate": "2022-01-12T14:08:26.219Z",
"id": "91f5ecd0-e18d-4029-8745-143323e02007",
"invoiceDate": "2022-01-12T14:08:26.580Z",
"openBalance": 53.13,
"paidAmount": 53.13,
"qbId": "",
"totalAmount": 53.13
}
]
}
]
this jq will get me my ids jq '.QueryResponse.Invoice | map(.Id)' which can be readily consumed by jq. The question now is (and this is what I don't know) how to randomly choose from this array and update the sample payload:
jq 'map(. + {
invoices : .invoices | map(. + {qbId: ??random here })
})
'
If I understood correctly, you want to replace each id field (spelling may differ, sometimes it's Id) with a randomly generated id string.
This solution first extracts the paths of all such id fields (in various spellings) using jq, then iterates over the result in the shell, using uuidgen to generate an id for each, which is fed into another jq call which uses setpath to change the value at the paths saved to the ids generated:
file="input.json"
jq -c '
paths(.. | scalars) | select(.[-1] == ("id", "Id", "ID")) | tojson
' "$file" |
while read -r json; do printf '["%s",%s]\n' "$(uuidgen)" "$json"; done |
jq -n --argfile file "$file" '
reduce inputs as [$id,$json] ($file; setpath($json | fromjson; $id))
'
[
{
"Id": "10162eb7-29ba-4b60-ad20-e5b1133eca63",
"Line": [
{
"Amount": 100,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "272832df-a8f5-4877-92de-1545150afc33",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "TAX"
},
"UnitPrice": 25
}
},
{
"Amount": 100,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
},
{
"Id": "190b0e50-e007-46a4-b1ca-c3efb762629c",
"Line": [
{
"Amount": 140,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "f7067227-56d4-4849-873a-3ee5c336999e",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 35,
"Description": "Pest Control Services",
"DetailType": "SalesItemLineDetail",
"Id": "181d7c6b-0afa-4f44-a568-2c482fc5c285",
"LineNum": 2,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Pest Control Services",
"value": "54"
},
"ItemRef": {
"name": "Pest Control",
"value": "10"
},
"Qty": 1,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 175,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
}
]
This shows how to select elements at random from an array, assuming a bash or sufficiently bash-like environment:
#!/bin/bash
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRnc '
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
# Input: an array
# Output: an array, being a selection of $k elements from . chosen at random without replacement
def prns($k):
if $k <= 0 then []
else . as $in
| length as $n
| if $k > $n then "no can do" | error
else ($n|prn) as $ix
| [$in[$ix]] + (($in[0:$ix] + $in[$ix+1:])|prns($k-1))
end
end;
# Two illustrations
# Three from range(0,10) (with replacement):
[range(0;10) | ( ["a", "b", "c"] | .[length|prn]) ],
# Three from an array, without replacement:
([range(0;10)] | prns(3))
'
request->
{
"data":
{
"type": "employee",
"attributes":
[
{
"name": "Sam",
"empId": 1,
"grade": "A"
},
{
"name": "Barsa",
"empId": 2,
"grade": "A"
},
"name": "Ram",
"empId": 3,
"grade": "B"
}
]
}
}
response->##
##
{
"data":
{
"type": "grade",
"employees":
[
{
"name": "Sam",
"empId": 1,
"grade": "A"
},
{
"name": "Barsa",
"empId": 2,
"grade": "A"
},
"name": "Ram",
"empId": 3,
"grade": "B"
}
]
}
}
Chapters Modifying a JSON message and SET statement explain how to do the following:
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
SET OutputRoot = InputRoot;
SET OutputRoot.JSON.Data.data.type = 'grade';
SET OutputRoot.JSON.Data.data.attributes NAME = 'employees';
RETURN TRUE;
END;
NOTE: Your request message is invalid json: The { before "name": "Ram", is missing.
I wrote this code to get the product information with it's images and category:
->where('category_id', 5)
->with('category', 'Files')->get();
my result is:
{
"id": 2,
"name": "test",
"price": 13000,
"description": "some text ...",
"shop_id": 1,
"rate": 0,
"category_id": 5,
"discount_percent": 20,
"category": {
"id": 5,
"name": "cat1",
"shop_id": 1
},
"files": [
{
"id": 99,
"disk_name": "5ef1af07d6d98778754621.jpg",
"file_name": "13330983_xl.jpg",
"file_size": 69813,
"content_type": "image/jpeg",
"title": null,
"description": null,
"field": "product_gallery",
"sort_order": 99,
"created_at": "2020-06-23 07:28:07",
"updated_at": "2020-06-23 07:28:10",
"path":...... storage/app/uploads/public/5ef/1af/07d/5ef1af07d6d98778754621.jpg",
"extension": "jpg"
}
]
}
now i want to access the path field, how can i do it?
i use this way for access but i don't get result:
products[0].files[0].path
You should use toArray() function to convert data likes this
->where('category_id', 5)
->with('category', 'Files')->get()->toArray();
And then access
products[0]['files'][0]['path']
I am having an array of json object which contain timing and data.
Basically, each element, contain timing, id and user as below
[
{
"id": "abc",
"ts": "2017-08-17T20:42:12.557229",
"userid": "seb"
},
{
"id": "def",
"ts": "2017-08-17T20:42:52.724773",
"userid": "seb"
},
{
"id": "ghi",
"ts": "2017-08-17T20:42:53.724773",
"userid": "matt"
},
{
"id": "jkl",
"ts": "2017-08-17T20:44:50.557229",
"userid": "seb"
},
{
"id": "mno",
"ts": "2017-08-17T20:44:51.724773",
"userid": "seb"
},
{
"id": "pqr",
"ts": "2017-08-17T20:50:52.724773",
"userid": "seb"
}
]
My goal is to remove object too close to each other if the userid is the same. if the time difference is below 2 sec, we remove the element.
From the list, I should get the list
[
{
"id": "abc",
"ts": "2017-08-17T20:42:12.557229",
"userid": "seb"
},
{
"id": "def",
"ts": "2017-08-17T20:42:52.724773",
"userid": "seb"
},
{
"id": "ghi",
"ts": "2017-08-17T20:42:53.724773",
"userid": "matt"
},
{
"id": "pqr",
"ts": "2017-08-17T20:50:52.724773",
"userid": "seb"
}
]
even if the 2 objects for user matt and seb are too close to each other below 2seconds, we have to keep the element as it's not the same user
"ts": "2017-08-17T20:42:52.724773" for seb
and
"ts": "2017-08-17T20:42:53.724773" for matt
Any idea how to code it in Ruby ? I always compared the element n to the n-1 and delete the n-1 if needed
require 'time'
result = []
timestamps = {}
data.each do |item|
ts = timestamps[item['userid']]
if ts.nil? or Time.parse(item['ts']) - Time.parse(ts) > 2
result.push(item)
timestamps[item['userid']] = item['ts']
end
end
puts result
What about the code below?
It changes the order of the records, but you could re-sort them if needed.
require 'date'
def time_elapsed_in_seconds(start_time, end_time)
((end_time - start_time) * 24 * 60 * 60).to_i
end
def too_close?(first_time, second_time, threshold = 2)
time_elapsed_in_seconds(first_time, second_time) < threshold
end
def datetimes(a, b)
return [DateTime.parse(a), DateTime.parse(b)]
end
def should_reject_record?(record, next_record)
datetimes = datetimes(record[:ts], next_record[:ts])
record[:userid] == next_record[:userid] && too_close?(*datetimes)
end
def filter_records(records)
sorted = records.sort_by{|record| [record[:userid], record[:ts]] }
sorted.select.with_index do |record, index|
previous_record = sorted[index-1]
record == sorted.first || !should_reject_record?(previous_record, record)
end
end
records = [
{
"id": "abc",
"ts": "2017-08-17T20:42:12.557229",
"userid": "seb"
},
{
"id": "def",
"ts": "2017-08-17T20:42:52.724773",
"userid": "seb"
},
{
"id": "ghi",
"ts": "2017-08-17T20:42:53.724773",
"userid": "matt"
},
{
"id": "jkl",
"ts": "2017-08-17T20:44:50.557229",
"userid": "seb"
},
{
"id": "mno",
"ts": "2017-08-17T20:44:51.724773",
"userid": "seb"
},
{
"id": "pqr",
"ts": "2017-08-17T20:50:52.724773",
"userid": "seb"
}
]
puts filter_records(records)