I want to parse this JSON data but I have no idea how can I parse it.
{
"kind": "Testing",
"data": {
"modhash": "fffffghhhhhhbf0bb4601d6158db548b5296632215a0",
"dist": 75,
"children": [
{
"kind": "z4",
"data": {
"subreddit": "iOS",
"selftext": "I am iOS developer",
"author_fullname": "tyy_iif5q",
"saved": false,
"gilded": 0,
"clicked": false,
"title": "When is a good time to use docker?",
"subreddit_name_prefixed": "r/webdev",
"hidden": false,
"pwls": 6,
"downs": 0,
"hide_score": false,
"name": "t3_gwqan4",
"quarantine": false,
"link_flair_text_color": "dark",
"upvote_ratio": 0.96,
"subreddit_type": "public",
"ups": 277,
"total_awards_received": 0,
"is_original_content": false,
"is_reddit_media_domain": false,
"is_meta": false,
"can_mod_post": false,
"score": 277,
"author_premium": false,
"thumbnail": "self",
"edited": false,
"is_self": true,
"created": 1591333752,
"link_flair_type": "text",
"wls": 6,
"author_flair_type": "text",
"domain": "self.webdev",
"allow_live_comments": false,
"selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>I just finished part 7 of the Full Stack Open course (<a href=\"https://www.fullstackopen.com/en\">www.fullstackopen.com/en</a>) and due to graphQL and typescript not being within the core content of the course, I thought I might try and learn docker. </p>\n\n<p>Turns out, trying to learn docker is just making me even more confused than when I didn't even know docker existed. </p>\n\n<p>​</p>\n\n<p>Could someone explain to me, when is a good time to use docker? How one would go about using docker, and when in someone's learning path would one ideally learn docker?</p>\n\n<p>​</p>\n\n<p>I am still learning react, databases, and overall front-end and back-end web development currently. The most I have done thus-far is hosting an app I made on heroku. I haven't setup a personal website, portfolio, or hosted an app on an independent website either.</p>\n</div><!-- SC_ON -->",
"archived": false,
"no_follow": false,
"is_crosspostable": true,
"pinned": false,
"over_18": false,
"media_only": false,
"can_gild": true,
"spoiler": false,
"locked": false,
"visited": false,
"subreddit_id": "t5_2qs0q",
"id": "gwqan4",
"is_robot_indexable": true,
"author": "Sensualities",
"num_comments": 127,
"send_replies": true,
"whitelist_status": "all_ads",
"contest_mode": false,
"author_patreon_flair": false,
"permalink": "/r/webdev/comments/gwqan4/when_is_a_good_time_to_use_docker/",
"parent_whitelist_status": "all_ads",
"stickied": false,
"url": "https://www.reddit.com/r/webdev/comments/gwqan4/when_is_a_good_time_to_use_docker/",
"subreddit_subscribers": 487697,
"created_utc": 1591304952,
"num_crossposts": 1,
"is_video": false
}
},
{
"kind": "t3",
"data": {
"subreddit": "iOS",
"selftext": "I am iOS developer",
"author_fullname": "tyy_iif5q",
"saved": false,
"gilded": 0,
"clicked": false,
"title": "When is a good time to use docker?",
"subreddit_name_prefixed": "r/webdev",
"hidden": false,
"pwls": 6,
"downs": 0,
"hide_score": false,
"name": "t3_gwqan4",
"quarantine": false,
"link_flair_text_color": "dark",
"upvote_ratio": 0.96,
"subreddit_type": "public",
"ups": 277,
"total_awards_received": 0,
"is_original_content": false,
"is_reddit_media_domain": false,
"is_meta": false,
"can_mod_post": false,
"score": 277,
"author_premium": false,
"thumbnail": "self",
"edited": false,
"is_self": true,
"created": 1591333752,
"link_flair_type": "text",
"wls": 6,
"author_flair_type": "text",
"domain": "self.webdev",
"allow_live_comments": false,
"selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>I just finished part 7 of the Full Stack Open course (<a href=\"https://www.fullstackopen.com/en\">www.fullstackopen.com/en</a>) and due to graphQL and typescript not being within the core content of the course, I thought I might try and learn docker. </p>\n\n<p>Turns out, trying to learn docker is just making me even more confused than when I didn't even know docker existed. </p>\n\n<p>​</p>\n\n<p>Could someone explain to me, when is a good time to use docker? How one would go about using docker, and when in someone's learning path would one ideally learn docker?</p>\n\n<p>​</p>\n\n<p>I am still learning react, databases, and overall front-end and back-end web development currently. The most I have done thus-far is hosting an app I made on heroku. I haven't setup a personal website, portfolio, or hosted an app on an independent website either.</p>\n</div><!-- SC_ON -->",
"archived": false,
"no_follow": false,
"is_crosspostable": true,
"pinned": false,
"over_18": false,
"media_only": false,
"can_gild": true,
"spoiler": false,
"locked": false,
"visited": false,
"subreddit_id": "t5_2qs0q",
"id": "gwqan4",
"is_robot_indexable": true,
"author": "Sensualities",
"num_comments": 127,
"send_replies": true,
"whitelist_status": "all_ads",
"contest_mode": false,
"author_patreon_flair": false,
"permalink": "/r/webdev/comments/gwqan4/when_is_a_good_time_to_use_docker/",
"parent_whitelist_status": "all_ads",
"stickied": false,
"url": "https://www.reddit.com/r/webdev/comments/gwqan4/when_is_a_good_time_to_use_docker/",
"subreddit_subscribers": 487697,
"created_utc": 1591304952,
"num_crossposts": 1,
"is_video": false
}
}
],
"after": "ggggggg"
}
}
I want to parse this JSON data but I have no idea how can I parse it.I worked with simple json but I have no idea how can I parse nested json please help me. I want to parse this JSON data but I have no idea how can I parse it.I worked with simple json but I have no idea how can I parse nested json please help me.
Take a look at this library, it will help you ;)
https://pub.dev/packages/json_serializable
This lib will generate conversion code fromJson and toJson for each model annotated with #JsonSerializable
It take care of nested objects, type conversion and many other things ;)
Use this link to prepare model class of Json
https://javiercbk.github.io/json_to_dart/
HttpResponse response = callApi;
then after getting response just use json.decode;
then use fromjson Method of your Model generated through link and your whole json is parsed
Map userMap = jsonDecode(jsonString);
var user = User.fromJson(userMap);
User is Model created from site
At first you need a classes representing JSON with the method fromJson which parses JSON into your object.
Also you should use nested classes for nested JSON fields
Alternatively, you could generate necessary classes with this class generator
class JSONRepresentation {
final String kind;
final Data data;
JSONRepresentation({this.kind, this.data});
JSONRepresentation .fromJson(Map<String, dynamic> data)
: kind = data['kind'],
data = Data .fromJson(data['data']);
}
class Data {
final String modhash;
final int dist;
//final List<AnotherDataClass> children;
final String after;
Data({this.modhash, this.dist, this.after});
Data .fromJson(Map<String, dynamic> data)
: modhash = data['modhash'],
dist = data['dist'],
after = data['after'];
}
And then you can parse it using dart:convert and get data from objects representing JSON as below
import "dart:convert";
void main() {
var jsonData =
'{"kind": "Testing",
"data": {
"modhash": "fffffghhhhhhbf0bb4601d6158db548b5296632215a0",
"dist": 75,
//"children": [...],
"after": "ggggggg"}}';
var parsedJson = json.decode(jsonData);
var jsonRepresentationObject = JSONRepresentation.fromJson(parsedJson);
//Example of getting data from objects representing JSON
print("kind: "+ jsonRepresentationObject.kind +
"\nData modhash: " + jsonRepresentationObject.data.modhash);
}
You could test my example here - online dart compiler
Related
I'm working with Snowflake and loading json files from a Staging environment to an ODS environment.
The structure of the json data looks like this:
{"address": "921 Pearl St",
"attributes": {
"Alcohol": "'beer_and_wine'",
"Ambience": "{'touristy': False, 'hipster': False, 'romantic': False, 'divey': False, 'intimate': False, 'trendy': False, 'upscale': False, 'classy': False, 'casual': True}",
"BikeParking": "True",
"BusinessAcceptsBitcoin": "False",
"BusinessAcceptsCreditCards": "True",
"BusinessParking": "{'garage': False, 'street': True, 'validated': False, 'lot': False, 'valet': False}"
}
Now I'm trying to query my data in Snowflake like this:
SELECT json_data:attributes.BusinessParking.garage
FROM my_json_table;
but all I get are null values.
Any idea how do I get to the bottom levels in the "BusinessParking" field?
thanks!!!
Trying to parse the JSON in online tool I got:
Error: Parse error on line 9:
..., 'valet': False}"}
----------------------^
Expecting '}', ',', got 'EOF'
You could check if it is correct by running TRY_PARSE_JSON:
SELECT TRY_PARSE_JSON(json_data) from my_json_table
Firstly your object is missing a closing }
So if I rewrite you "JSON" to be valid then the select form you use works:
select parse_json('{
"address": "921 Pearl St",
"attributes": {
"Alcohol": "beer_and_wine",
"Ambience": {
"touristy": False,
"hipster": False,
"romantic": False,
"divey": False,
"intimate": False,
"trendy": False,
"upscale": False,
"classy": False,
"casual": True
},
"BikeParking": True,
"BusinessAcceptsBitcoin": "False",
"BusinessAcceptsCreditCards": "True",
"BusinessParking": {
"garage": False,
"street": True,
"validated": False,
"lot": False,
"valet": False
}
}
}') as json_data,
json_data:attributes.BusinessParking.garage,
json_data:attributes:BusinessParking:garage
;
but as that implies your JSON is not valid.
looking at your Ambience it a stringfied JSON sub-object, not an object, thus the embedded ' inside it. So escaping your '`' in the sub strings, to it parses correctly we will need to PARSE_JSON the sub-objects, thus:
select parse_json('{"address": "921 Pearl St",
"attributes": {
"Alcohol": "\'beer_and_wine\'",
"Ambience": "{\'touristy\': False, \'hipster\': False, \'romantic\': False, \'divey\': False, \'intimate\': False, \'trendy\': False, \'upscale\': False, \'classy\': False, \'casual\': True}",
"BikeParking": "True",
"BusinessAcceptsBitcoin": "False",
"BusinessAcceptsCreditCards": "True",
"BusinessParking": "{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}"
}}') as json_data,
PARSE_JSON(json_data:attributes.BusinessParking):garage
gives the answer false that you are looking for.
EDIT
to add more details, you object has json objects nestsed which are Stringify'ed
to put replicate that the above (and below) SQL uses escape tokens \ to allow single quotes into the data. But after that we can select json_data:attributes.BusinessParking and see it presented like you have it shown in your example, which mean if we then PARSE_JSON on that, we get a valid JSON object again, which we can then access via :garage
select parse_json('{"address": "921 Pearl St",
"attributes": {
"Alcohol": "\'beer_and_wine\'",
"Ambience": "{\'touristy\': False, \'hipster\': False, \'romantic\': False, \'divey\': False, \'intimate\': False, \'trendy\': False, \'upscale\': False, \'classy\': False, \'casual\': True}",
"BikeParking": "True",
"BusinessAcceptsBitcoin": "False",
"BusinessAcceptsCreditCards": "True",
"BusinessParking": "{\'garage\': False, \'street\': True, \'validated\': False, \'lot\': False, \'valet\': False}"
}}') as json_data,
json_data:attributes.BusinessParking as it_looks_the_same,
PARSE_JSON(it_looks_the_same) as valid_json,
valid_json:garage
which gives:
After a lot of research I'm able to query the fields the way I needed to.
I found out that I need to use the "try_parse_json" functions with those fields as they are defined as strings not actual json objects.
After using the function I can parse specific fields just like this:
SELECT
TRY_PARSE_JSON(
jsondata: attributes.BusinessParking
): garage AS garage
FROM
"RESTAURANT"."STAGING"."JSON_TABLE_BUSINESS"
Snowflake's doc isn't that great but somehow it explains what you can do: https://docs.snowflake.com/en/sql-reference/functions/try_parse_json.html
This is another way you could transform string values to columns in case you need it: https://sqlkover.com/cool-stuff-in-snowflake-part-3-split-and-flatten/
Thanks for all the comments #simeon-pilgrim!
I am having a issue with a MySQL table design. Originally, this data comes from a JSON dataset that records all items that a customer as added into their cart along with other information. My problem is figuring out how I should store this data. The goal is to be able to query all parts that a user has added and compare it to the final cart contents to see what items have been added and removed. This may also be a query solvable problem but I am fairly new to MySQL.
Also, this data needs to be in MySQL.
Thanks for any help!
Here is a small example of my parts table:
Here is an example of the JSON, The file is arounf 1 gb so it is a bit hard to find a good example that I can fit in the question.
{
"account_num": "TEMPZVNVB",
"loginid": 70196,
"itemavail": {
"3": "In Stock",
"2": "In Stock",
"1": "In Stock"
},
"ads_country": "US",
"englishsessionredirected": false,
"isloginok": true,
"returnpage": "/newweb/gauges/convection_kjlc_2.cfm",
"accounts": "CreditCard",
"shoppingcart": [
{
"ReturnPage": "/newweb/",
"Availability": "In Stock",
"subtotal": 78.4,
"description": "GASKET, COPPER",
"PartNo": "0800",
"Custom": false,
"unitprice": 78.400,
"ArrayName": "Session.CartItems1",
"extprice": 78.400,
"Checked": false,
"EmailNeeded": false,
"Quantity": 1,
"FromSaved": false
},
{
"ReturnPage": "/newweb",
"Availability": "In Stock",
"subtotal": 27.35,
"description": "10/PKG",
"PartNo": "0275",
"Custom": false,
"unitprice": 27.350,
"ArrayName": "Session.CartItems2",
"extprice": 27.350,
"Checked": false,
"EmailNeeded": false,
"Quantity": 1,
"FromSaved": false
},
{
"ReturnPage": "/newweb",
"Availability": "In Stock",
"subtotal": 385.0,
"description": "CONVECTION ",
"PartNo": "03LL",
"Custom": false,
"unitprice": 385.000,
"ArrayName": "Session.CartItems3",
"extprice": 385.000,
"Checked": false,
"EmailNeeded": false,
"Quantity": 1,
"FromSaved": false
}
I'm trying to migrate an old Plone 3.3 site that uses
FileSystemStorage using Mikko's Simple JSON export script.
Everything runs fine, except for the absence of a value on the image fields, which are returned as empty strings (''):
[
...
{
"allowDiscussion": false,
"contributors": [],
"creation_date": "2009-11-04T15:15:36-02:00",
"creators": [
"johndoe"
],
"description": "",
"effectiveDate": null,
"excludeFromNav": false,
"expirationDate": null,
"id": "banner_vertical.jpg",
"image": "",
"language": "",
"location": "",
"modification_date": "2009-11-04T15:15:37-02:00",
"portal_type": "Image",
"relatedItems": [],
"rights": "",
"subject": [],
"title": "Banner vertical",
"urlLegend": "http://"
},
...
]
Any hint?
(iw.fss version used is 2.8.0rc5).
FSS was not known or used by the site for which the script has been made.
You might want to retrofit convert() and other methods to support your use case.
Consider a social network. It has posts. For feed, you request /feed and get the list of posts.
In the UI, there are things to show for a post, like if the user liked the post or not, if the user starred it or not, etc. These things don't look like they belong inside the post object.
Another case is when you fetch the likes. The frontend needs to know if the user in each 'like' object is being followed or not.
Where to put this info in the response JSON?
Its depends on your application and which data you want to show to the user. For ex,consider you are listing a user's feeds. In that feed,you want to show
Message
Liked by the current user or not(i don't know the difference between liked and stared)
Number of likes
List of liked users.
shared by the user or not
Shared count
List ofShared users.
etc..
In the above list,
Some data need two api fetch to get complete info and some not. For example,"List of liked users","List of Shared users". This is generally a dynamic data module. You have to get those details in a separate api for better performance of the server and also data integrity.
In some cases,some apps needs sneak peek of the liked shared users info in the listing page. In that case,you can include the some fixed small number of users details in the same list /feeds response itself and include the "See More(like Facebook)" option in the UI.
Some static singular data(single column data) can be list in the initial get /feeds itself.
I wonder why don't you follow the same twitter's list tweets style,
https://dev.twitter.com/rest/reference/get/search/tweets
{
"coordinates": null,
"favorited": false,
"truncated": false,
"created_at": "Fri Sep 21 23:40:54 +0000 2012",
"id_str": "249292149810667520",
"entities": {
"urls": [
],
"hashtags": [
{
"text": "FreeBandNames",
"indices": [
20,
34
]
}
],
"user_mentions": [
]
},
"in_reply_to_user_id_str": null,
"contributors": null,
"text": "Thee Namaste Nerdz. #FreeBandNames",
"metadata": {
"iso_language_code": "pl",
"result_type": "recent"
},
"retweet_count": 0,
"in_reply_to_status_id_str": null,
"id": 249292149810667520,
"geo": null,
"retweeted": false,
"in_reply_to_user_id": null,
"place": null,
"user":
{
"profile_sidebar_fill_color": "DDFFCC",
"profile_sidebar_border_color": "BDDCAD",
"profile_background_tile": true,
"name": "Chaz Martenstein",
"profile_image_url": "http://a0.twimg.com/profile_images/447958234/Lichtenstein_normal.jpg",
"created_at": "Tue Apr 07 19:05:07 +0000 2009",
"location": "Durham, NC",
"follow_request_sent": null,
"profile_link_color": "0084B4",
"is_translator": false,
"id_str": "29516238",
"entities": {
"url": {
"urls": [
{
"expanded_url": null,
"url": "http://bullcityrecords.com/wnng/",
"indices": [
0,
32
]
}
]
},
"description": {
"urls": [
]
}
},
"default_profile": false,
"contributors_enabled": false,
"favourites_count": 8,
"url": "http://bullcityrecords.com/wnng/",
"profile_image_url_https": "https://si0.twimg.com/profile_images/447958234/Lichtenstein_normal.jpg",
"utc_offset": -18000,
"id": 29516238,
"profile_use_background_image": true,
"listed_count": 118,
"profile_text_color": "333333",
"lang": "en",
"followers_count": 2052,
"protected": false,
"notifications": null,
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/9423277/background_tile.bmp",
"profile_background_color": "9AE4E8",
"verified": false,
"geo_enabled": false,
"time_zone": "Eastern Time (US & Canada)",
"description": "You will come to Durham, North Carolina. I will sell you some records then, here in Durham, North Carolina. Fun will happen.",
"default_profile_image": false,
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/9423277/background_tile.bmp",
"statuses_count": 7579,
"friends_count": 348,
"following": null,
"show_all_inline_media": true,
"screen_name": "bullcityrecords"
},
"in_reply_to_screen_name": null,
"source": "web",
"in_reply_to_status_id": null
}
You have two options:
Make a separate API method for getting information about user context data - /api/users/1/feeds/1 .Pay attention, that this option will force you to send request per feed. So, if you'll have 1000 feeds - you will have 1000 + 1 request (so called N+1 problem).
As for me - it's not a good idea.
You can store user data in json, for example is this way:
{
"feedName": "feed1",
...
"currentUser": {
"liked": true,
"starred": true
}
}
By using this option you will avoid N+1 requests problem in your RESTful service
For all the users, the post resource should be the same. Adding specific user context info inside it seems like polluting it
I can see where you're coming from and I quite agree.
Ivan's 1st solution should not be used as he already mentioned, his 2nd is better but then if you GET the posts JSON which should contain only post objects, there is also this currentUser that doesn't really belong there.
My suggestion is that for each post you keep track of which users have liked and/or starred it, etc. Then you keep a clean structure while still having the info you need available in the same request/response.
Example
GET /feed HTTP/1.1
[
{
"text": "hello world, im a post!",
"author": "Jack",
"likes": 3,
"likedBy": [
"John",
"James",
"Jessica"
],
"stars": 2,
"starredBy": [
"John",
"Mary"
]
},
{
"text": "hello world, im also a post! :D",
"author": "Mary",
"likes": 1,
"likedBy": [
"James"
],
"stars": 0,
"starredBy": [
]
},
]
Where each {} object represents a post object.
On the client side, you could then check if the likedBy list contains the currently logged in user and proceed with the result as you see fit. Same for stars and any other of these properties a post might have.
can anyone help in finding xpath of 'tenantId' which is in JSON response given below
{
"statusCode": "200",
"errorParams": null,
"payLoad": {
"UGX:XCD": {
"id": 42802,
"fromCurrency": {
"tenantId": "2a610af6-2e42-4a78-8e04-98755e2c6113",
"code": "UGX",
"name": "Ugandan Shilling",
"isoCode": "UGX",
"active": true,
"enabled": true,
"flagModifiedName": false,
"erpId": null,
"createdOn": 1427155200000,
"fieldLibraryStatus": "DRAFT",
"userId": null
},
"toCurrency": {
"tenantId": "2a610af6-2e42-4a78-8e04-98755e2c6113",
"code": "XCD",
"name": "East Caribbean Dollar",
"isoCode": "XCD",
"active": true,
"enabled": true,
"flagModifiedName": true,
"erpId": null,
"createdOn": 1427155200000,
"fieldLibraryStatus": "DRAFT",
"userId": "c7f68b05-044d-44e1-8fc5-0f97a4ece91b"
},
"rate": 33,
"inverseRate": 0.0303,
"createdOn": 1434025926983,
"modifiedOn": 1434326400000,
"active": true,
"purpose": "cmd",
"enabled": true,
"tenantFlag": true,
"erpId": null
}
How to traverse till 'tenantId' ??
Thanks in Advance.
SoapUI internally converts almost anything to XML. You will have to use the ResponseAsXml property. In the GUI, if you click on the XML tab, you can see this representation.
Something like the following should work:
${test_step#ResponseAsXml#//*:fromCurrency/*:tenantId}
${test_step#ResponseAsXml#//*:toCurrency/*:tenantId}
If you want to handle this using groovy, then extend SOAPUI with GSON.jar library provided by Google.
this library specially written to handle the JSON objects.
With this you can rip every individual objects and its attribute-values easily how complex may the JSON object be, a nice add-on to work with.