Passing json text to api - arrays

{
"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.

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.

Extracting a json string from child array in bash

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'

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

AngularJS prepare data for MultiBar Chart NVD3

I am calling my API to get JSON array below,
array = [{"date":1533791167870,"count":1,"name":"James"},{"date":1533791167870,"count":3,"name":"Kane"},{"date":1533791167918,"count":1,"name":"Kane"},{"date":1536203856526,"count":2,"name":"Trent"},{"date":1536217871371,"count":5,"name":"Kane"},{"date":1536217882525,"count":4,"name":"James"}]
And my name array,
name_array = ["Kane","Trent","James","Sam"]
I am using Lodash library. I would like to get my output array such that for each date all names are mapped from name_array and if there is count then get count value from array otherwise 0. I am expecting my output array as below result_array,
result_array = [{
"date": 1533791167870,
"count": 1,
"name": "James"
}, {
"date": 1533791167870,
"count": 3,
"name": "Kane"
},
{
"date": 1533791167870,
"count": 0,
"name": "Trent"
},
{
"date": 1533791167870,
"count": 0,
"name": "Sam"
}, {
"date": 1533791167918,
"count": 0,
"name": "James"
},
{
"date": 1533791167918,
"count": 1,
"name": "Kane"
},
{
"date": 1533791167918,
"count": 0,
"name": "Sam"
},
{
"date": 1533791167918,
"count": 0,
"name": "Trent"
}, {
"date": 1536203856526,
"count": 0,
"name": "James"
},
{
"date": 1536203856526,
"count": 0,
"name": "Kane"
},
{
"date": 1536203856526,
"count": 2,
"name": "Trent"
},
{
"date": 1536203856526,
"count": 0,
"name": "Sam"
}, {
"date": 1536217871371,
"count": 0,
"name": "James"
},
{
"date": 1536217871371,
"count": 5,
"name": "Kane"
},
{
"date": 1536217871371,
"count": 5,
"name": "Trent"
},
{
"date": 1536217871371,
"count": 5,
"name": "Sam"
}, {
"date": 1536217882525,
"count": 4,
"name": "James"
},
{
"date": 1536217882525,
"count": 0,
"name": "Trent"
}, {
"date": 1536217882525,
"count": 0,
"name": "Sam"
},
{
"date": 1536217882525,
"count": 0,
"name": "Kane"
}
]
I am doing this way for NVD3 AngularJS MultiBar chart. Since my array data is not working for the stacked view but group view is working fine.
I am using AngularJS, Lodash.
I would appreciate the help.
You can do it with several _.map calls:
let result = _.flatten(_.map(_.groupBy(array, 'date'), (existedEntries, date) => {
let usedNames = _.map(existedEntries, entry => entry.name);
let namesDiff = _.difference(name_array, usedNames);
let dateNumeric = parseFloat(date);
let enrichedNames = _.map(namesDiff, name => {
return {
date: dateNumeric,
count: 0,
name: name
};
});
return _.union(existedEntries, enrichedNames);
}));
First of all, we group existed entries by date, and for each group determine what names are used there. Then we calculate difference between all possible names and names used in the group. For each name from the difference we map it into default object with count: 0 and date equals to the date of current existing group. Next, we union existing values of the group with ones we got after enriching names diff. Final flatten call removes away temporal internal array structure.
In case if you use ES5 or lower, replace arrows syntax sugar with functions and let with var, i.e. var usedNames = _.map(existedEntries, function(entry) {return entry.name;});
P.S. In ES6 you can replace Lodash map function with eponymous native function, i.e. let usedNames = existedEntries.map(entry => entry.name);

Resources