I am attempting to parse out the following json array below. The activities object is a string and I have successfully been able to parse it out with the following in jq.
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"activities": "{\"hero\": \"batman\", \"weapon\": \"sword\", \"ts\": null, \"remote\": \"1\",\"game\": \"joker\", \"image\": \"bold\"}"
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"activities": "{\"hero\": \"superman\", \"weapon\": \"cape\", \"ts\": null, \"remote\": \"2\",\"game\": \"batman\", \"image\": \"kind\"}"
}
]
The following command allows me to parse out the string object but I haven't quite figured out how to get it back into the original array.
jq '.[].activities | fromjson?' test.json
Output:
{
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
}
{
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
I tried several variances of the += and |= operators to add back to the original without any luck.
jq '.[].activities |= fromjson? | select( . != "")'
Bypassed any empty values with the select command.
This yields no results for the activities object when I run it. It brings the original id, created_at, and updated_at forward but nothing else.
Appreciate any help you can offer. Thanks guys.
Preferred Output
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
]
Something like this should be enough:
map(del(.activities) + (.activities | fromjson))
Online demo
Here's a starting point, assigning a real JSON object to the activities key:
map(.activities |= fromjson)
Output:
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"activities": {
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
}
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"activities": {
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
}
]
And to merge the activities sub-object into its parent object, while deleting the old property:
map(. + (.activities | fromjson) | del(.activities))
Output:
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
]
Related
I got a collection but I want to sort that collection. If the two application's cgpa is equal then sort the list collection according to the annual_salary where lower will up at list. Default the collection sort with the cgpa. below is my server response.
Thanks in advance
"applications": [
{
"id": 1,
"name": "Dr. W Khan",
"annual_salary": 5000,
"created_at": "2022-11-07T19:16:01.000000Z",
"updated_at": "2022-11-07T19:16:01.000000Z",
"education": [
{
"id": 1,
"application_id": 1,
"name": "HSC",
"cgpa": 400,
"created_at": "2022-11-07T19:16:01.000000Z",
"updated_at": "2022-11-07T19:16:01.000000Z"
}
]
},
{
"id": 2,
"name": "Dr. M Khan",
"annual_salary": 7000,
"created_at": "2022-11-07T19:16:14.000000Z",
"updated_at": "2022-11-07T19:16:14.000000Z",
"education": [
{
"id": 2,
"application_id": 2,
"name": "HSC",
"cgpa": 350,
"created_at": "2022-11-07T19:16:14.000000Z",
"updated_at": "2022-11-07T19:16:14.000000Z"
}
]
},
{
"id": 3,
"name": "Dr.",
"annual_salary": 5000,
"created_at": "2022-11-07T19:16:28.000000Z",
"updated_at": "2022-11-07T19:16:28.000000Z",
"education": [
{
"id": 3,
"application_id": 3,
"name": "HSC",
"cgpa": 350,
"created_at": "2022-11-07T19:16:28.000000Z",
"updated_at": "2022-11-07T19:16:28.000000Z"
}
]
}
]
This is the query for me.
$application = Application::with(
[
'education' => function ($query) {
$query->orderBy('cgpa', 'desc');
},
]
)
->orderBy('annual_salary', 'asc')
->get();
But ordering the list followed by a second orderBy.
I have the following JSON which have an element array_within_array in another array. So I want to pull both array_within_array element and upper array element(event_array) to root level using jq.
So here there are 3 events.
meal_selection
login
placed_order
Event placed_order have two sub-events in array. So after conversion there should be 4 events(1 from meal_selection, 1 from login and 2 from placed_order). these all should be on the same level.
Here is the JSON
{
"region": "USA",
"user_id": "123",
"event_array": [{
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
},
{
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
},
{
"event_attributes": {
"array_within_array": [
{
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
{
"date": "2021-08-18",
"category": "dinner",
"location": "home"
}
]
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
]
}
and I want to convert to the below one
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
}
{
"region": "USA",
"user_id": "123",
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-18",
"category": "dinner",
"location": "home"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
Here's a straightforward solution that keeps things simple by using two steps:
{ region, user_id} + (.event_array[] )
| if .event_attributes|has("array_within_array")
then .event_attributes.array_within_array as $a
| .event_attributes = $a[]
else .
end
How can I retrieve all this information from Json and insert it into a ListView on my Flutter app?
"acordos": {
"29": {
"0": {
"installment": 0,
"link": "https://hml.use...",
"amount": 55.07,
"amount_paid": 0.0,
"status": "VEN",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-05"
},
"1": {
"installment": 1,
"link": "https://hml.use...",
"amount": 20.62,
"amount_paid": 20.62,
"status": "PAG",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-04"
},
"2": {
"installment": 2,
"link": "https://hml.use...",
"amount": 20.62,
"amount_paid": 20.62,
"status": "PAG",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-04"
},
"3": {
"installment": 3,
"link": "https://hml.use...",
"amount": 20.62,
"amount_paid": 20.62,
"status": "PAG",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-04"
}
}
},
I have did it before, but only with json files with "[]" in the objects and I has success but with this kind of Json I can't.
The JSON format is wrong, try add {} in the beginning and the end, like
{
"acordos": {
"29": {
"0": {
"installment": 0,
"link": "https://hml.use...",
"amount": 55.07,
"amount_paid": 0.0,
"status": "VEN",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-05"
},
"1": {
"installment": 1,
"link": "https://hml.use...",
"amount": 20.62,
"amount_paid": 20.62,
"status": "PAG",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-04"
},
"2": {
"installment": 2,
"link": "https://hml.use...",
"amount": 20.62,
"amount_paid": 20.62,
"status": "PAG",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-04"
},
"3": {
"installment": 3,
"link": "https://hml.use...",
"amount": 20.62,
"amount_paid": 20.62,
"status": "PAG",
"due_date": "2021-05-04",
"receivable_type": "ACO",
"payment_method": "banking_billet",
"acordo": "86",
"created_at": "2021-05-04",
"updated_at": "2021-05-04"
}
}
}
}
Then, you may decode it correctlly
There's only one "type" of JSON, and the one you wrote is not correct.
If you want to write correct JSON, there's one simple rule:
"property":value
The value can be an array [], a string "", an integer, etc.
If what you wrote is a "custom structure", then you should write a specific interpreter in order to reach the last element that is a correct written JSON.
The problem is in the last section... It's an array but no [] are defined, so it's not a correct JSON and you cannot use something already existing to read it but you need to iterate through elements by yourself and then at the end you can get a JSON object from the last {}
i have the following Template model and TestCategory hasMany Test:
class Template extends Model
{
protected $table = 'test_templates';
protected $fillable = [
'customers_id',
'tests_categories_id',
'tests_id'
];
public function customers(){
return $this->belongsTo(Customer::class, 'customers_id');
}
public function testCategories(){
return $this->belongsTo(TestCategory::class, 'tests_categories_id');
}
public function tests(){
return $this->belongsTo(Test::class, 'tests_id');
}
}
And I have the following index method defined in TemplateController
public function index($customerId)
{
$templates = Template::with( 'customers','testCategories','tests')
->where('customers_id', $customerId)->get();
$templates = $templates->groupBy('tests_categories_id');
$templates = json_decode($templates, true);
$templates = array_values($templates);
return response()->json(['Status' => True, 'template' => $templates]);
}
I got the following json output:
{
"Status": true,
"template": [
[
{
"id": 1,
"customers_id": 1,
"tests_categories_id": 1,
"tests_id": 1,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "9842********",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 1,
"name": "General Tests",
"created_at": null,
"updated_at": null
},
"tests": {
"id": 1,
"tests_category_id": 1,
"name": "Lamination",
"created_at": null,
"updated_at": null
}
},
{
"id": 2,
"customers_id": 1,
"tests_categories_id": 1,
"tests_id": 2,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984*******",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 1,
"name": "General Tests",
"created_at": null,
"updated_at": null
},
"tests": {
"id": 2,
"tests_category_id": 1,
"name": "Paper Type",
"created_at": null,
"updated_at": null
}
}
],
[
{
"id": 7,
"customers_id": 1,
"tests_categories_id": 2,
"tests_id": 8,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984******",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 2,
"name": "Scratch Test",
"created_at": null,
"updated_at": null
},
"tests": {
"id": 8,
"tests_category_id": 2,
"name": "HRN Visibility",
"created_at": null,
"updated_at": null
}
}
]
]
}
I want to collect all the tests as per the test_categories without reprinting customers and test_categories as following:
{
"Status": true,
"template": [
[
{
"id": 1,
"customers_id": 1,
"tests_categories_id": 1,
"tests_id": 1,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984*****",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 1,
"name": "General Tests",
"created_at": null,
"updated_at": null
},
"tests": {
{
"id": 1,
"tests_category_id": 1,
"name": "Lamination",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"tests_category_id": 1,
"name": "Paper Type",
"created_at": null,
"updated_at": null
}
}
}
],
[
{
"id": 7,
"customers_id": 1,
"tests_categories_id": 2,
"tests_id": 8,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984265*****",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 2,
"name": "Scratch Test",
"created_at": null,
"updated_at": null
},
"tests": {
{
"id": 8,
"tests_category_id": 2,
"name": "HRN Visibility",
"created_at": null,
"updated_at": null
}
}
}
]
]
}
If I understand the problem correctly the tests are being separated into different templates when you are expecting some of them to be related to the same template.
This is because your Template class is storing the test_id that it belongs to and it can only store 1 test per Template. So when you are calling Template::with('tests') it can only return one Test::class.
Because you expect to see that many Tests belongTo one Template, and when you look at the relationship from the Template to Test, you expect that a Template has many Tests. Therefore, your Template class should implement the hasMany relationship like so:
class Template extends Model
{
protected $fillable = [
'customers_id',
'tests_categories_id' // notice the removal of the test_id which should also be removed from the test_templates migration
];
...
public function tests(){
return $this->hasMany(Test::class); //change belongsTo to hasMany
}
...
}
Instead of storing the test_id on the Template class you should be storing the template_id on your Test class and look something like so:
class Test extends Model
{
protected $fillable = [
...
'test_template_id' // This should be added to $fillable and the tests migration
...
];
...
public function template(){
return $this->belongsTo(Template::class, test_template_id);
}
...
}
Now when you call Template::with('tests') it can return many Test::class and should match your expected output.
I have a great problem for parse a very difficult json string. For Eg
{
"facilityDetails": [
{
"tableName": "FACILITY",
"facilityDetails": [
{
"id": 1,
"itemId": "s101",
"name": "facility",
"status": 1,
"lastEditedOn": "01/Jan/201200: 00: 00.000"
}
]
},
{
"tableName": "PLACE_SERVICE",
"facilityDetails": [
{
"id": 1,
"itemId": "22",
"name": "placeservice",
"facility": "5",
"status": 1,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 2,
"itemId": "55",
"name": "placeservice",
"facility": "t",
"status": 2,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 3,
"itemId": "99",
"name": "placeservice",
"facility": "r",
"status": 33,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 4,
"itemId": "22",
"name": "placeservice",
"facility": "",
"status": 0,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
}
]
},
{
"tableName": "AGENT",
"facilityDetails": [
{
"agentId": 2,
"itemId": "1",
"name": "agent",
"defUnitId": 0,
"defRouteId": 0,
"color": "",
"synonyms": "",
"administrationType": 0,
"status": 0,
"lastEditedOn": "01/Jan/201200: 00: 00.000"
}
]
}
]
}
for this json string "facilityDetails" is an inner array that have different details at each time. How can i parse this type of json. If anyone know please help me
You should be able to parse this with the Json.NET library.
http://james.newtonking.com/projects/json-net.aspx