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
How to Close Sweet Alert on Ajax Request Completion
Trying to Add Image to Pdf Using Jspdf
React Enable Button After All Form Fields Are Not Empty
Text Box to Appear When a Radio Button Is Selected
Puppeteer Wait Until Page Is Completely Loaded
Disable Click Outside of Bootstrap Modal Area to Close Modal
Jquery - Ajax Returning Error 500 on Some Posts
How to Instantiate a JavaScript Class in Another Js File
How to Get 30 Days Prior to Current Date
Get Image from Json File Using JavaScript and Display in HTML Img Tag
How to Add Query String Parameter to Same Url When on Click a Href Link
How to Change Font-Size in Vuetify Component
Filter Json Object to String Array by Key Value
Update Value in Localstorage Array by Existing Key
How to Make Select Option Disable
Vuejs Set Active Class, When One Li Element Clicked in V-For Loop