Extracting a json string from child array in bash - arrays

This is the output I receive via curl. I want to save 'playcount' in a variable.
How do I loop through tracks[] to get the strings 'playcount' and save them so I can form the sum? I've spend a lot of time figuring it out with jq but nothing really worked out..
{
"success": true,
"data": {
"track_count": 2,
"discs": [
{
"number": 1,
"name": "",
"tracks": [
{
"album_id": "u2hh2n2b7fo20v46cbe",
"playcount": 17212,
"name": "World Savior",
"number": 1,
"duration": 341,
},
{
"album_id": "jk299sdhjahj991nbwd",
"playcount": 9812,
"name": "Tower",
"number": 2,
"duration": 281,
}
]
}
],
"month": 11,
"day": 26,
"year": 2021,
"type": "single",
}
}

If you want the list of playcount values, you just have to drill down to the right content:
jq '.data.discs[].tracks[].playcount'
If you want their sum, make an array of that and add its entries:
jq '[.data.discs[].tracks[].playcount]|add'

Related

needs to added json parent array name to every sub array

Flutter / Dart Question
This is the array which I have , In the Accessories list it contains brand name("samsung"), I need to added the brand name to all the sub array.
you can see the second code space in the sub array of Brand name key it contains brand name of the parent key
{
"Accessories": [
{"id": 1,
"brand": "samsung",
"parentId": null,
"children": [
{
"id": 4,
"name": "Ace",
"parentId": 1
},
{
"id": 5,
"name": "note",
"parentId": 1
},
{
"id": 6,
"name": "galaxy",
"parentId": 1
}
]
},
{"id": 2,
"name": "Asus",
"parentId": null,
"children": [
{
"id": 7,
"name": "gaming",
"parentId": 2
},
{
"id": 8,
"name": "office",
"parentId": 2
}
]
},
]
}
what I expecting result is below
{
"phones": [
{
"id": 1,
"brand": "samsung",
"parentId": null,
"children": [
{
"id": 4,
"name": "Ace",
*"brand": "samsung",*
"parentId": 1
},
{
"id": 5,
"name": "note",
*"brand": "samsung",*
"parentId": 1
},
{
"id": 6,
"name": "galaxy",
*"brand": "samsung",*
"parentId": 1
}
]
},
{
"id": 2,
"name": "Asus",
"parentId": null,
"children": [
{
"id": 7,
"name": "gaming",
*"name": "Asus",*
"parentId": 2
},
{
"id": 8,
"name": "office",
*"name": "Asus",*
"parentId": 2
}
]
},
]
}
please help me to resolve the question .
..............................................................................................................................
Assuming you are not using any class to encode and decode json and you just want to get from the first json string to the second json string:
Map<String, dynamic> decodedMap = json.decode(accessoriesJson);
final List accessories = decodedMap['Accessories'];
final List result = accessories.map((a) {
return {
...a,
'children': (a['children'] as List)
.map((c) => ({...c, 'parent': a['brand']}))
.toList(),
};
}).toList();
final resultJson = json.encode({
'phones': result,
});
Note: I assumed the name of the parent on the first json is called brand (Like in the samsung example). Although in the Asus example it's called name.
You can create json decoder/encoder class with https://app.quicktype.io/
I used the same to convert the json to Accessories type.
After that you can use the forEach two times to update the existing array without creating a new one.
Like this
var list = Accessories.fromMap(json);
print(list.accessories);
list.accessories!.forEach((e) {
var brand = e.brand;
if (brand != null) {
e.children?.forEach((element) {
element.brand = brand;
});
}
});
print(list.toMap());
I am only using "brand" because
You should keep your property name same if using array.
Using "name" in parent will add property "name" in child. Which is wrong because there is already a property named "name" in child. So it will replace the child name.
Working example
https://dartpad.dev/?id=a7f4a869d02db3b929e3814db28fcbe1&null_safety=true
I had to make changes for null handling in type class as I don't know what will be the optional in this array. So I made everything optional.

Avoid using arrays (and [0]) in multidimensional JSON object

I'm storing data in a JSON file and need different objects/arrays. So far I am using the following structure:
data= [
{
"savedRuns": [
{
"id": 1,
"name": "Run 1"
},
{
"id": 2,
"name": "Run 2"
},
{
"id": 3,
"name": "Run 3"
}
]
},
{
"groups": [
{
"id": 1,
"name": "g1"
},
{
"id": 2,
"name": "g2"
},
{
"id": 3,
"name": "g3"
}
]
},
{
"locations": [
{
"id": 1,
"name": "home"
},
{
"id": 2,
"name": "work"
},
{
"id": 3,
"name": "school"
}
]
}
]
I would like to access the data in the file in an easy way, for instance:
console.log(data.savedRuns)
console.log(data.locations)
Unfortunately this returns undefined and the only way to access the data is:
console.log(data[0].savedRuns);
console.log(data[2].locations);
Since I don't necessarily know the position of the object, I would like to avoid that. If there a way around this, or a different structure to adopt for my file?
Link to JSFiddle
You have each attribute in a separate object, stored in an array. Get rid of the outer array and store the attributes in one object, then use data.savedRuns.
Thanks to Marks answer I could get the right syntax. Posting below in case of interest to others.
data = {
"savedRuns": [{
"id": 1,
"name": "Run 1"
},
{
"id": 2,
"name": "Run 2"
},
{
"id": 3,
"name": "Run 3"
}
],
"groups": [{
"id": 1,
"name": "g1"
},
{
"id": 2,
"name": "g2"
},
{
"id": 3,
"name": "g3"
}
],
"locations": [{
"id": 1,
"name": "home"
},
{
"id": 2,
"name": "work"
},
{
"id": 3,
"name": "school"
}
]
}

How to parse JSON array objects to string array objects in JMeter

I have the following JSON. I want the JSON array objects of technicalSettings (two objects in this case but can vary based on the API response) into a string array without loosing any text and want to loop through the string array to add few more elements and to form a new JSON and store the new JSON in a string variable.
{
"data": {
"statusCode": 200,
"success": true,
"technicalSettings": [
{
"program": "C:/temp/abc.exe",
"actions": "9",
"file_name": "abc1",
"new_file_name": "newabc1",
"version": "2.0.0.0",
"product_name": "abc",
"description": "abc",
"eventdate": "20160601120000",
"autoVoiceProfile": {
"autoVoices": [
{
"autoVoiceLanguage": 0,
"autoVoiceMessage": [
{
"name": "AV1",
"duration": "1.200000",
"checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
"fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
"id": 4
},
{
"name": "AV1",
"duration": "0.600000",
"checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
"fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
"id": 4
},
{
"name": "AV2",
"duration": "2.800000",
"checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
"fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
"id": 5
},
{
"name": "AV2",
"duration": "4.100000",
"checksum": "c5a6a39df38505c0c22b75d9ea7781a1755e9c8c9f435e08034f579361ba751c",
"fileName": "/usr/g/db/user_autoVoiceMsg10.aifc",
"id": 5
}
]
}
],
"messagesitefilename": null
}
},
{
"program": "C:/temp/abc.exe",
"actions": "9",
"file_name": "abc2",
"new_file_name": "newabc2",
"version": "2.0.0.0",
"product_name": "abc",
"description": "abc",
"eventdate": "20160601120000",
"autoVoiceProfile": {
"autoVoices": [
{
"autoVoiceLanguage": 0,
"autoVoiceMessage": [
{
"name": "AV1",
"duration": "1.200000",
"checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
"fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
"id": 4
},
{
"name": "AV1",
"duration": "0.600000",
"checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
"fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
"id": 4
},
{
"name": "AV2",
"duration": "2.800000",
"checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
"fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
"id": 5
}
]
}
],
"messagesitefilename": null
}
}
],
"library": {
"version": 6,
"dmIdVersion": 5
}
},
"success": true,
"statusCode": 200,
"errorMessage": ""
}
I used the JSON Extractor but it is failing when split into array since the array objects contains multiple ",".
String strPublishTechSettings = "${pPublishTechSettings_ALL}";
String[] PublishTechSettings = strPublishTechSettings.split(",");
Don't inline JMeter Functions or Variables into scripts as:
in case of compilation caching enabled only first value will be used for all iterations
it conflicts with Groovy GString Template Feature
it might be resolved into something causing compilation failure or unexpected behaviour
so change this line:
String strPublishTechSettings = "${pPublishTechSettings_ALL}";
to this one:
String strPublishTechSettings = "${pPublishTechSettings_ALL}";
and your test should start working as expected:
in the above example vars stands for JMeterVariables class instance, see JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on other JMeter API shorthands available to JSR223 Test Elements

How to access a field in relation in eloquent orm

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']

Passing json text to api

{
"data": {
"id": 41
},
"payments": {
"data": [
{
"id": 5,
"invoice_id": 41,
"amount": 12,
"account": {
"data": {
"id": 1,
"company_id": 1,
"name": "Cash",
"current_balance": 12,
"bank_name": "Cash"
}
},
"currency": {
"data": {
"id": 5,
"company_id": 1,
"code": "USD",
"precision": "2",
"symbol": "$"
}
}
}
]
}
}
Postman gives this json file for GET request. How can I pass this as json text to an API? When I pass same data displayed here it won't work. In payments section there is an array for "data", What should be the format to pass a json text in this case?
Choose POST method. Click on body tab. There will be option raw , paste the JSON there and hit send.

Resources