Parsing JSON data in flutter that has a null key - json

I have a json data that I used a website to parse(this is the site: https://javiercbk.github.io/json_to_dart/ ) but I got an error that the model was not correct and that is because my json data contains a list that has a list and in that list, another list that contains the keys and their values but there is a key which is null in that list..I put the json data below for better understanding..My question is, "is there any way that I can create a model and skip over the null key and work with the other available keys and values?"
{
"result": {
"newsfeed": [
[
null,
{
"author": "5f177c2139e3390017e71c70",
"userTag": "",
"text": "try this one",
"video": "",
"image": "",
"createdAt": "Wed Sep 16 2020 09:28:00 GMT+0000 (Coordinated Universal Time)",
"name": "",
"campus": ""
}
],
[
null,
{
"author": "5f177d2739e3390017e71c71",
"userTag": "bigjo",
"text": "yooooo",
"video": "",
"image": "",
"createdAt": "Wed Sep 16 2020 09:28:00 GMT+0000 (Coordinated Universal Time)",
"name": "",
"campus": "Bells University Of Technology"
}
]
]
}
}

Well you can assign some other value to it instead of null.
x['result']['newsfeed'].forEach((element){
element[0] = element[1]; //or any other object
}
This might not be the best way to do it though.

I also just had such issues. You can use QuickType to generate the mapping code for you based on the JSON input.
I am using factory for JSON decoding and for optional values the line would be something like
taskPic: parsedJson["taskPic"] == null ? null : parsedJson['taskPic'],

Related

Optimum Serializing in Django Rest Framework and parsing in Javascript

I am trying to adapt an event calendar vuejs module called dayspan calendar. Current entry object for an event as json is a bit strange and I want to balance the parsing of the payload before the post request and handling of data in DRF serializers. So I can get an optimum and performant client-server rest API communication. Json output is as below before any parsing:
{
"data": {
"title": "YOK",
"description": "",
"location": "",
"color": "#1976d2",
"forecolor": "#ffffff",
"calendar": "",
"busy": true,
"icon": ""
},
"schedule": {
"times": [
"17"
],
"dayOfMonth": [
11
],
"year": [
2021
],
"month": [
5
]
}
}
There are more schedule fields like "dayOFWeek", "duration" etc. that is to be expected for different entries.
What would be the best approach in terms of parsing json payload before posting to DRF and in deserializing stage before saving into database? I appreciate any ideas.

ReactJS material-UI tableData to parse nested JSON

My client is ReactJS and server is Java Jersey based.
Java Jersey produces the following nested JSON.
[
{
"projectname": "BMI",
"testRun": "5934391890034305",
"numberOfTestcases": "3",
"timeNowString": "Mon Sep 21 21:17:34 IST 2020",
"oidobjList": [
{
"oid": "5f68cb16f01c7607230d1fcd"
},
{
"oid": "5f68cb16f01c7607230d1fcf"
},
{
"oid": "5f68cb16f01c7607230d1fd1"
}
],
"fileNameObjList": [
{
"fileName": "Basispath_BMI_0_out.gif"
},
{
"fileName": "Basispath_BMI_1_out.gif"
},
{
"fileName": "Basispath_BMI_2_out.gif"
}
]
},
{
"projectname": "BMI",
"testRun": "3320691551029718",
"numberOfTestcases": "3",
"timeNowString": "Mon Sep 21 18:37:54 IST 2020",
"oidobjList": [
{
"oid": "5f68a5aaf01ca8f40b42a4e7"
},
{
"oid": "5f68a5aaf01ca8f40b42a4e9"
},
{
"oid": "5f68a5aaf01ca8f40b42a4eb"
}
],
"fileNameObjList": [
{
"fileName": "Basispath_BMI_0_out.gif"
},
{
"fileName": "Basispath_BMI_1_out.gif"
},
{
"fileName": "Basispath_BMI_2_out.gif"
}
]
}
]
My ReactJS code is as follows:
{this.props.articles.map((el, index) => (
<Table
tableHeaderColor="primary"
tableHead={["Parameter", "Value"]}
tableData={[
["Project Name: ", el.projectname],
["Test Run: ", el.testRun],
["No Of TestCases: ", el.numberOfTestcases],
// ["File Name: ", el.fileNameObjList],
["Time Stamp: ", el.timeNowString],
]}
/>
))}
The response received in client side is attached in the image
Questions:
How to render nested JSON elements in ReactJS material-ui based tableData?
How to get filenames using map and index?
Please provide solution to this issue.
1 - First you need to decide how you will render your table. If you just want to render a single table for all your nested arrays, you can generate a single array of it and render normally. You can also render multipla tables, one for each nested array, it's your choice as a programmer.
2 - You can get filenames field by doing an simple map in fileNameObjList. But if you want a single array of filenames, you can either use Array.reduce or combine the mapped arrays with Array.concat or by pushing every item with an for loop
and also, I'm not sure but i don't think Material UI table is used that way. You may be using another data-table component that uses MUI

How can I select some JSON object having name "forecast_XXX" using XPATH\JSONPATH?

I am not so into XPATH and I have the following problem.
I have this JSON object:
{
"forecast_1": {
"country": "Rwanda",
"forecast_date": "2018-03-20",
"province": "Kigali City",
"district": "Kigali",
"morning": {
"min_temp": 14,
"status": "Sun with right rain",
"max_temp": 16,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "Lorem ipsum"
},
"afternoon": {
"min_temp": 24,
"status": "Dark cloud with rain",
"max_temp": 28,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "Lorem ipsum"
}
},
"forecast_2": {
"country": "Rwanda",
"forecast_date": "2018-03-25",
"province": "Kigali",
"district": "Kigali",
"morning": {
"min_temp": 21,
"status": "Rain showers",
"max_temp": 21,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "There will be heavy rain"
},
"afternoon": {
"min_temp": 32,
"status": "Rain showers",
"max_temp": 32,
"humidity": "",
"wind_direction": "",
"wind_force": "",
"description": "There will be heavy rain with thunder"
}
}
}
as you can see it contains 2 "root" object identified by forecast_XXX (but can be more than 2), in the specific case I have forecast_1 and forecast_2.
So I can have a collection of JSON objects like these but there are not into a JSON array (they come from an external web service and I can not have them inside an array).
So I need an XPATH expression that identifies all these forecast_XXX objects (so I can try to iterate on these objects as if thse object were inside an array).
How can I select all these forecast_XXX objects using XPATH expression? And where can I test it? (it should exist some online tool where I put the JSON and my XPATH expression and I obtain the output)
For selection into JSON structures using XPath, you need XPath 3.1. (I'll leave a JsonPath answer to others.)
This use of forecast_N keys is an unfortunate way of doing the data design, but it can be handled as:
map:keys()[$json, matches('.', 'forecast_[0-9]+')]!map:get($json, .)
Assuming $json is the result of parsing the JSON input using parse-json() or json-doc().
If your XPath 3.1 engine supports higher order functions then you could also do
map:for-each($json,
function($k, $v){$v[matches($k, 'forecast_[0-9]+')]})

How to preserve Json Object Order in Robot Framework

I am working with an API that requires the json key value pairs to be ordered when creating resources. The API provides a method (called new) that allows you to make a GET request that will return an object model. I would like to update the model with values within my RobotFramework test cases. Is there a native way within Robot Framework to make a GET request and preserve the json object order sent by the server? Here's an example of the JSON response to the GET new method:
{
"account": {
"#id": "",
"#uri": "",
"#oldID": "",
"person": {
"#id": "",
"#uri": ""
},
"accountType": {
"#id": "",
"#uri": "",
"name": null
},
"accountName": "",
"createdDate": null,
"createdByPerson": {
"#id": "",
"#uri": ""
},
"lastUpdatedDate": null,
"lastUpdatedByPerson": {
"#id": "",
"#uri": ""
}
}
}
If I use the following, the key values automatically get sorted:
${r}= GET Request MySession /accounts/new
For anyone else who needs ordered JSON in robot framework, I was able to achieve it with the following (thanks to help in the comments):
&{object}= Evaluate json.loads('''${r.text}''', object_pairs_hook=collections.OrderedDict) modules=json, collections
Though, ultimately this kind of logic is probably best suited in a custom helper library.

loop through simple json object

how do I loop through this json object to get each item value? I know this is easy but I need to undestand why there is two brackets ([]) in the first and end of this json object.
[// I'm talking about this
[
{
"id": 2,
"title": "xxxxxxxxx",
"author": "mike123",
"postdate": "March 12, 2013 at 6:46 pm",
"postdatecreation": "2013-03-12",
"posteditdate": null,
"postcontent": "eeeeee",
"userID": 34
}
]
]// and this
if I remove them the json still remain valid.
you can loop through your json object using $.each loop. here is the fiddle:
http://jsfiddle.net/Ay2UB/
here results is an array of array of an object. i've passed that object by accessing index
results[0] will give you an array
results[0][0] will give you object
code below:
var results=[
[
{
"id": 2,
"title": "xxxxxxxxx",
"author": "mike123",
"postdate": "March 12, 2013 at 6:46 pm",
"postdatecreation": "2013-03-12",
"posteditdate": null,
"postcontent": "eeeeee",
"userID": 34
}
]
]
$.each(results[0][0],function(key, value){
alert(value);
});
$.each can be used to loop through array or objects follow below link for more information:
http://api.jquery.com/jQuery.each/
Hope it helps. please correct me if I'm wrong.