Search Key in Nested json in Dart/Flutter - flutter

I want to search the key "AylaHeartBeatFrequency" inside nested json. how to search and get its value ? in flutter/dart
{
"sAWSIoT": {
"CloudStatus": 1,
"PairingFlag": 0,
},
"sDebug": {
"LocalDebugMsg_d": "Model ID",
"AylaHeartBeatFrequency": 0
},
"Product": {
"Mode": 1,
"Model": "abc"
},
"data": {
"DeviceType": 300,
"Endpoint": 0,
"UniID": "0000000000000000"
}
}

You can do:
var map = /*put your json here. You can use json.decode() on the json if it's not yet formatted*/, value;
for(var item in map.values) {
if(item.containsKey('AylaHeartBeatFrequency') value = item['AylaHeartBeatFrequency']) ;
}

Related

How to update jarray in jsonb field?

I have a jsonb field that contained the something like below:
How to update is_read properties in extras node to true where the users_pid = 1 and is_read=false?
I have tried below:
UPDATE chats
SET attributes = jsonb_set(
cast(attributes->'data'->>'extras' AS jsonb),
array['is_read'],
to_jsonb(true)
)
WHERE users_pid =1
AND cast(attributes->'data'->'extras_to'- >>'is_read' AS boolean) = false
but nothing updated
[
{
"data": {
"users_pid": 1,
"datetime": "2022-05-01 13:10:58",
"extras": {
"is_read": false,
"read_dt": ""
}
}
},
{
"data": {
"users_pid": 3,
"datetime": "2022-05-23 11:03:22",
"extras": {
"is_read": false,
"read_dt": ""
}
}
},
{
"data": {
"users_pid": 1,
"datetime": "2022-05-13 11:23:22",
"extras": {
"is_read": false,
"read_dt": ""
}
}
}
]

How json array pages by id dart

how all pages to map title print?
how json select example id=12 to map print title
{
"result": {
"name": "json1",
"pages": [
{
"zones": [
{
"title": "title1"
},
{
"title": "title2"
}
],
"id": 4
},
{
"zones": [
{
"title": "title3"
},
{
"title": "title4"
}
],
"id": 12
}
],
"creatorUserName": "admin",
"id": 2
}
}
List post = json;
children: post
.map( (post) => Container(
child: Center(child: Text(post.title]),)
))
.toList(),
I make a code to parse your json
var data = json.decode(jsonData);
var pagesArray = data["result"]["pages"];
pagesArray.forEach((page) {
var zones = page["zones"];
//Each id have your titles
int id = page["id"];
List<String> titles = new List();
zones.forEach((zone) {
titles.add(zone["title"]);
});
print("Id $id have this titles : ${titles.toString()}");
});

How to find average using map reduce in MongoDB?

My document is of format:
{
"PItems": {
"Workspaces": [
{
"Key": "Item1",
"Size": 228.399,
"Foo": "bar"
},
{
"Key": "Item2",
"Size": 111.399,
"Bar": "baz"
},
{
"Key": "Item2",
"Size": 636.66,
"Baz": "foo"
}
]
}
}
I need to find the average size of each items from all the documents in the collection. How do I do that?
Expected output:
Item1: 346.12
Item2: 563.58
I have tried the following:
db.Resources.mapReduce(
function() {
this.PItems.Workspaces.forEach(
function(z) {
emit(z.Key, z.Size);
}
);
},
function(item, space) {
var total = 0;
for ( var i=0; i<space.length; i++ )
total += size[i];
return { avg : total/space.length };
},
out: { inline: 1 }
);
I am getting a syntax error: SyntaxError: missing ) after argument list. What am I missing here?

How can I paginate using Resource<T> using Page<T>

I am using the following code to create a paginated list of JSON data for Artist objects:
#RequestMapping(value = "/artists/all", method = RequestMethod.GET, produces = {"application/hal+json"})
public Resources<Artist> getArtists(Pageable pageable) {
final Page<Artist> allArtists= artistService.GetAllArtists(pageable);
for (final Artist artist : allArtists) {
Long artistId = artist.getArtistId();
Link selfLink = linkTo(methodOn(ArtistController.class).getAllArtistById(artistId)).withSelfRel();
artist.add(selfLink);
final Link ordersLink = linkTo(methodOn(MusicController.class).getMusicByArtistId(artistId, pageable)).withRel("musics");
artist.add(ordersLink);
}
Link link =linkTo(ArtistController.class).withSelfRel();
Resources<Artist> result = new Resources<>(allArtists,link);
return result;
}
The code currently returns outputs that are in this format:
{
"artistId": 2,
"name": "Simon Mburu",
"_links": {
"self": {
"href": "http://localhost:8000/api/v1/artists/2"
},
"musics": {
"href": "http://localhost:8000/api/v1/musics/artist/2"
}
}
}
However my intent is to have the code return an output like this:
"pageable": {
"sort": {
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageSize": 20,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 2,
"size": 20
"number": 0,
"numberOfElements": 2
"sort": {
// ETC...
What changes can I make to my code to get it to output the data contained in the above example instead of what it currently outputs?

Rearrrange populated json result in mongoose

A simple json response for Post.find().populate("name") will return json result as follow. Note: The focus of the question is to rearrange the "name":"Alex" in json to the final structure as shown. Ignore the part that need hiding _id and __v. Thanks.
[
{
"_id": "54cd6669d3e0fb1b302e54e6",
"title": "Hello World",
"postedBy": {
"_id": "54cd6669d3e0fb1b302e54e4",
"name": "Alex",
"__v": 0
},
"__v": 0
},
...
]
How could i rearrange and display the entire json as follow?
[
{
"_id": "54cd6669d3e0fb1b302e54e6",
"title": "Hello World",
"name": "Alex"
},
...
]
You can use the lean() method to return a pure JSON object (not a mongoose document) that you can then manipulate using lodash helper methods such as map(), like in the following example:
Post.find()
.populate("name")
.lean().exec(function (err, result) {
if(result){
var posts = _.map(result, function(p) {
p.name = p.postedBy.name;
p.postedBy = undefined;
return p;
});
console.log(posts);
}
});
You can disable the "__v" attribute in your Schema definitions by setting the versionKey option to false. For example:
var postSchema = new Schema({ ... attributes ... }, { versionKey: false });
As follow-up to your question on rearranging the order of the properties in the JSON, JS does not define the order of the properties of an object. However, you
can use both the JSON.parse() and JSON.stringify() methods to change the order, for example
var json = {
"name": "Alex",
"title": "Hello World",
"_id": "54cd6669d3e0fb1b302e54e6"
};
console.log(json);
//change order to _id, title, name
var changed = JSON.parse(JSON.stringify(json, ["_id","title","name"] , 4));
console.log(k);
Check the demo below.
var json = {
"name": "Alex",
"title": "Hello World",
"_id": "54cd6669d3e0fb1b302e54e6"
};
//change order to _id, title, name
var changed = JSON.parse(JSON.stringify(json, ["_id","title","name"] , 4));
pre.innerHTML = "original: " + JSON.stringify(json, null, 4) + "</br>Ordered: " + JSON.stringify(changed, null, 4);
<pre id="pre"></pre>