Retrieving Json Response Data With Dynamic Key

Retrieving JSON response data with dynamic key

Try accessing the object like this :

const obj = this.props.data;

obj[Object.keys(obj)[0]].dataset1.date

Reference: How to access the first property of an object in Javascript?

How to access dynamic key values of a JSON object in node.js?

let data = [  {    "assetCategory": {       "canCreate": false,       "canView": false,       "canUpdate": false,       "canDelete": false,       "isMenu": false,       "parent": "settings"     }  }]

let unknownNames = data.map( item => Object.keys(item)) // returns ['assetCategory']//Returns array of all the unknown namesconsole.log(unknownNames);

get value from json with dynamic key

var t={'color':'red'}; // dynamic json data.
for(n in t)
{
alert(n);// n = key
var val =t[n];// value where key is n

}

Getting values from nested JSON with a dynamic key in a database

If you want one row per nested item, you can use json_each()

select b.id, 
t.uid,
t.item ->> 'path' as path,
t.item ->> '__id' as item_id,
t.item ->> 'state' as state
from bp_instances b
cross join json_each(body -> '__tasks') as t(uid, item)
WHERE body ->> '__templateId' = '6666f537-a370-4ebd-b709-ccb1d04b2da3'

Based on your sample data this returns:

id | uid                                  | path                                        | item_id                              | state      
---+--------------------------------------+---------------------------------------------+--------------------------------------+------------
1 | e689f2c9-e6c1-432b-8dec-968e75c8350b | /items/fbdeab2c-14cd-4209-8883-11673bdd3780 | e689f2c9-e6c1-432b-8dec-968e75c8350b | in_progress
1 | e689f2c9-e6c1-432b-8dec-968e75c8350b | /items/fbdeab2c-14cd-4209-8883-11673bdd3780 | e689f2c9-e6c1-432b-8dec-968e75c8350b | in_progress
1 | e689f2c9-e6c1-432b-8dec-968e75c8350b | /items/fbdeab2c-14cd-4209-8883-11673bdd3780 | e689f2c9-e6c1-432b-8dec-968e75c8350b | in_progress

JSON Get the name of dynamically changing key with PHP

Can you provide a couple more examples?
Or try this code and let us know if it breaks

<?php

$json='{
"_text": "this is a test",
"entities": {
"dynamic_key": [
{
"confidence": 0.99,
"value": "thi is the answer"
}
]
},
"msg_id": "1234532123"
}';


$data=json_decode($json,true);

foreach ($data['entities'] as $key=>$val)
{
echo "VALUE IS $key\n values are ";
var_dump($val);


}

flutter problem in json parsing of dynamic key values

Just check out the example that I have made for you based on the json that you provided.

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:json_parsing_example/models.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
double value;

String json = '''
{
"success": true,
"data": {
"ones": [{
"id": "2",
"username": "LM10002"
},
{
"id": "6",
"username": "LM10006"
}
],
"twos": [{
"id": "3",
"username": "LM10003"
},
{
"id": "8",
"username": "LM10008"
}
],
"threes": [{
"id": "4",
"username": "LM10004"
}],
"fours": [{
"id": "5",
"username": "LM10005"
},
{
"id": "14",
"username": "GT10014"
}
]
}
}

''';
@override
void initState() {
super.initState();

getData();
}

getData() {
Map mapValue = jsonDecode(json);
// This is where you iterate via the data object
// that is the value which is key,value pair
List<Data> data = List();
mapValue['data'].forEach((key, value) {
List<User> user = List();
value.forEach((item) {
user.add(User(id: item['id'], username: item['username']));
});
data.add(Data(name: key, userList: user));
});

data.forEach((element) {
print(element.name + " : " + '${element.userList.length}');
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(child: Text('s')),
);
}
}

class Data {
final String name;
final List<User> userList;

Data({this.name, this.userList});
}

class User {
final String id;
final String username;

User({this.id, this.username});
}

Let me know if it works.



Related Topics



Leave a reply



Submit