Parsing a very complex json response in dart - json

I'm trying to load a json file from a server response and parsing it in flutter, the model i create is working for all the other fields but i'm in trouble with this class
this is a part of the JSON response:
"episodes": {
"1": [
{
"id": "63",
"episode_num": 1,
"title": "Some Name",
"container_extension": "mp4",
"info": {
"director": "",
"plot": "",
"cast": "",
"rating": "",
"releasedate": "",
"movie_image": "",
"genre": "",
"duration_secs": 6495,
"duration": "01:48:15"
}
}
]
}
in this case the entry under episodes is just one but this will represents a season and all the episode inside it, so under episodes many of this entry (undefined number during coding) can be present
At this time, using online json to dart converter tools i can be able to retrive just this one entry but if a response have more than 1 season i can't see it.
There is any way to handle this?
EDIT:
Solved using a for cicle with max value = (json['episodes'].length + 1).
For the info stored inside each 'episodes' value i can use
json['episodes']['$i']

Valid JSON is always convertible to a Dart data structure. But what you may be asking is "can I get nested objects from this?", and that just depends on how hard you want to work. Some JSON-to-Dart tools are better than others and some JSON values are impossible for any automated tool to make sense of. Only real answer is: "it depends".

Related

Add data to a json file using Talend

I have the following JSON:
[
{
"date": "29/11/2021",
"Name": "jack",
},
{
"date": "30/11/2021",
"Name": "Adam",
},
"date": "27/11/2021",
"Name": "james",
}
]
Using Talend, I wanna add 2 lines to have something like:
[
{
"company": "AMA",
"service": "BI",
"date": "29/11/2021",
"Name": "jack",
},
{
"company": "AMA",
"service": "BI",
"date": "30/11/2021",
"Name": "Adam",
},
"company": "AMA",
"service": "BI",
"date": "27/11/2021",
"Name": "james",
}
]
Currently, I use 3 components (tJSONDocOpen, tFixedFlowInput, tJSONDocOutput) but I can't have the right configuration of components in order to get the job done !
If you are not comfortable with json .
Just do these steps :
In the metaData just create a FileJson like this then paste it in your job as a tFileInputJson
Your job design and mapping would be
In your tFileOutputJson don't forget to change in the name of the data block "Data" with ""
What you need to do there according to the Talend practices is read your JSON. Then extract each object of it, add your properties and finally rebuild your JSON in a file.
An efficient way to do this is using tMap componenent like this.
The first tFileInputJSON will have to specify what properties it has to read from the JSON by setting your 2 objects in the mapping field.
Then the tMap will simply add 2 columns to your main stream, here is an example with hard coded string values. Depending on you needs, this component will also offer you the possibility to assign dynamic data to your 2 new columns, it's a powerful tool for manipulating the structure of a data stream.
You will find more infos about this component in the official documentation : https://help.talend.com/r/en-US/7.3/tmap/tmap; especially the "tMap scenarios" part.
Note
Instead of using the tMap, if you are comfortable with Java, you can use a tjavaRow instead. Using this, you can setup your 2 new columns with whatever java code you want to put as long as you have defined the output schema of the component.
output_row.Name = input_row.Name;
output_row.date = input_row.date;
output_row.company = "AMA";
output_row.service = "BI";

Processing JSON Records in Cold Fusion

First time post, so please pardon my format. Will try to provide as much detail as I can. I'm using CF 2018 and receiving a JSON file with data around employees. I can handle the API call to get it, but need some advice on how to best process it.
Here's the JSON file:
{
"Report_Entry": [
{
"Account_Group": "HEALTH GROUP",
"Group": "SERVICES GROUP",
"Employee_ID": "111111",
"Legal_Name_-_First_Name": "Ted",
"Worker_Skills_as_Text": "Programmer",
"Certification": "ICAgile Certified Professional (ICP) - The International Consortium for Agile",
"primaryWorkEmail": "ted_smith#abc.com"
},
{
"Account_Group": "FINANCE GROUP",
"Group": "INNOVATION TEAM",
"Employee_ID": "222222",
"Legal_Name_-_First_Name": "Mary",
"Worker_Skills_as_Text": "Analyst",
"Certification": "PMP; Writing Master",
"primaryWorkEmail": "mary_smith#abc.com"
},
{
"Account_Group": "ENERGY GROUP",
"Group": "BUSINESS DEVELOPMENT TEAM",
"Employee_ID": "333333",
"Legal_Name_-_First_Name": "John",
"Worker_Skills_as_Text": "Developer",
"Certification": "Certified Scrum Master (CSM) - Scrum Alliance; Certified Scrum Product Owner (CSPO) - Scrum Alliance",
"primaryWorkEmail": "john_smith#abc.com"
}
]
}
When I deserialize it returns as as:
I need to be able to loop through the structure, arrays, and the structure to capture the values then process them (i.e., insert into a table, compare to existing data, etc.). I know I need to loop through the entire thing but can't come up with the right syntax with error.
Any help or examples would be greatly appreciated.
Thanks
Jim
Here is what I've been using for dealing with something similar:
Based on your dump, you have a struct with an array of structs.
the following is a snippet that should return the first element of the array, which is a struct:
myStruct = Report_entry[1];
for (currentKey in myStruct) {
writedump(currentKey);
writedump(myStruct[currentKey]);
writeoutput('<hr>');
}
you want to get the syntax of looping over arrays and looping over structs.
CFDUMP is your friend.
I'd be happy to help with syntax. I recognize I did not provide a complete solution, but you are a little closer.

What is the best way to parse a complex JSON array?

I have a JSON object which is very complex and very big size. I know how to parse or get values. But I want to learn what is the fastest way to filter data from that JSON?
The actual JSON is very big and complex. To make it simple, I just created a sample JSON which looks like the follwoing. I want to filter only the "CompanyTitle" which is only locatated like "NY".
{
"Companies": [
{
"Url": "www.abc.com",
"CompanyTitle": "title of ABC",
"OfficeLocations": [
"Online",
"NY",
"CO"
],
"OfficeLocationsDisplay": "Campus/Online"
},
{
"Url": "www.xyz.com",
"CompanyTitle": "title of xyz",
"OfficeLocations": [
"CO",
"NY",
"IL"
],
"OfficeLocationsDisplay": "Campus/Online"
}]
}
Note: I already completed the parsing but it is very slow. So, I want to learn if there is any fastest way to figure out, I will use that instead of my parsing.
This JSON is loaded on page from a .NET Model. So, I need to do it form the same page.
Thanks

Representing child objects in JSON for a mobile API

I am designing a JSON API for a mobile app and have to decide how to show child objects from the server to the client. Typically the request from the client to the server will be a single request to sync and the response will include all the objects that need updating. What is the best way to show the objects?
Option A - Nested Children:
{ "articles": [
{ "id" : 1,
"title": "This is the first article",
"comments": [
{"id": "1",
"article_id" : "1",
"title": "A comment on the first article"
}]
},
{ "id" : 2,
"title": "This is the second article",
"comments": [
{"id": "2",
"article_id" : "2",
"title": "A comment on the second article"
}]
}, ]}
OPTION B - All Objects on Their Own
{ "articles": [
{ "id" : 1,
"title": "This is the first article",
}
{ "id" : 2,
"title": "This is the second article",
}]
"comments": [
{"id": "1",
"article_id" : "1",
"title": "A comment on the first article"
},
{"id": "2",
"article_id" : "2",
"title": "A comment on the second article"
}]}
On the client side I can handle either format and build the relationship based on the article_id field so I am not too sure why nest the children, other than that it makes it look nice. However, when I think about writing tests for the client-side, especially the mapping of json to objects it seems easier to show and map each object on its own. I am a beginner here so any thoughts would be helpful.
PS. I am building the server using Rails/Grape and the clients with RestKit/Coredata (iOS) and probably RoboSpice/ORMLite (Android).
That's very subjective. There isn't one correct answer for that. It really depends on whatever approach is more suited to your task and data. You say this is a request used to sync data. How is the data represented and stored on the client side? If flat, like a relational database, then the flat output is probably easier to use. On the other hand, if the client will use the relationships a lot, it's probably better to use the nested structure.
From an API design standpoint, I'd have the endpoint for the articles collection accept a query parameter like expand, with a level number, or named entities, and it would add the nested children accordingly. So, for instance, GET /api/articles?expand=comments would generate output with nested comments, or GET /api/articles?expand=1 to generate output with all immediate children. That way, clients can easily generate the nested output if they need it, or they can query the endpoints for articles and comments separately and concatenate the output if they need the flat data.,

Best way to structure json data with backbone

I'm just about to write a web app using backbone and want to know what's the best way to structure my json file? I've read that using 'dictionary' arrays are best but was wondering if there's a better way of structuring the data. An example of how the data should be structure would be great too.
My data seems to have a lot of nested arrays and these seem to be hard to search through.
JSON has two types of relevant container objects.
Object and Array.
Object is probably what you're thinking of when you say "dictionary array".
You probably want an object with arrays of objects :)
{"courses": [{
"name": "Spanish 101",
"subject": "How to speak Spanish",
}, {
"name": "Introduction to Film",
"subject": "Make pretty films",
}, {
"name": "Social Psychology",
"subject": "Why people are weird.",
}],
"sections": [],
"modules": [],
"topics": [],
"lessons": [],
// etc..
}
Each of the [] items would be field with numerous objects.
Once you get this data into your APP (either JSONP, AJAX, or if it's just assinged to a variable in our page) you can put them in your Backbone collections using the reset function (See: http://backbonejs.org/#Collection-reset):
var Courses = new Backbone.Collection;
function processData(data) {
Courses.reset(data.courses);
// etc...
}