Looping a nesty struture in Coldfusion [closed] - json

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
Here is my JSON Data:
This i am trying to loop and get all the information about every node of it with the key name and the key data
{
"src": 1,
"feeds": [{
"id": 1,
"name": "All",
"polarPlot": true
}],
"srcFeed": 1,
"showSil": true,
"showFlg": true,
"showPic": true,
"flgH": 20,
"flgW": 85,
"acList": [{
"Id": 12589424,
"Rcvr": 1,
"HasSig": false,
"Icao": "C01970",
"Bad": false,
"Reg": "C-FJQL",
"FSeen": "\/Date(1672216397120)\/",
"TSecs": 334,
"CMsgs": 330,
"Alt": 34975,
"GAlt": 35206,
"InHg": 30.15059,
"AltT": 0,
"Call": "ROU1958",
"Lat": 45.59019,
"Long": -74.90746,
"PosTime": 1672216731130,
"Mlat": false,
"Tisb": false,
"Spd": 469.0,
"Trak": 59.3,
"TrkH": false,
"Type": "A321",
"Mdl": "Airbus A321 211SL",
"Man": "Airbus",
"CNum": "7117",
"From": "YYZ Lester B. Pearson, Toronto, Canada",
"To": "YQB Quebec Jean Lesage, Canada",
"Op": "Air Canada Rouge",
"OpIcao": "ROU",
"Sqk": "2222",
"Help": false,
"Vsi": 0,
"VsiT": 0,
"WTC": 2,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 2,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "2016"
}, {
"Id": 12595324,
"Rcvr": 1,
"HasSig": false,
"Icao": "C0307C",
"Bad": false,
"Reg": "C-FSJJ",
"FSeen": "\/Date(1672215986531)\/",
"TSecs": 745,
"CMsgs": 284,
"Alt": 35000,
"GAlt": 35189,
"InHg": 30.1092529,
"AltT": 0,
"Call": "ACA394",
"Lat": 47.86931,
"Long": -72.90211,
"PosTime": 1672216731064,
"Mlat": false,
"Tisb": false,
"Spd": 513.0,
"Trak": 117.2,
"TrkH": false,
"Type": "B38M",
"Mdl": "Boeing 737MAX 8",
"Man": "Boeing",
"CNum": "61217",
"From": "YYC Calgary, Canada",
"To": "YHZ Halifax / Stanfield, Canada",
"Op": "Air Canada",
"OpIcao": "ACA",
"Sqk": "1144",
"Help": false,
"Vsi": 0,
"VsiT": 0,
"WTC": 2,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 2,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "2018"
}, {
"Id": 12601386,
"Rcvr": 1,
"HasSig": false,
"Icao": "C0482A",
"Bad": false,
"Reg": "C-GBIN",
"FSeen": "\/Date(1672215747131)\/",
"TSecs": 984,
"CMsgs": 5124,
"Alt": 32925,
"GAlt": 33185,
"InHg": 30.1801186,
"AltT": 0,
"Call": "ROU1689",
"Lat": 45.119546,
"Long": -71.834575,
"PosTime": 1672216731191,
"Mlat": false,
"Tisb": false,
"Spd": 462.5,
"Trak": 31.1,
"TrkH": false,
"Type": "A319",
"Mdl": "Airbus A319 114",
"Man": "Airbus",
"CNum": "845",
"From": "FLL Fort Lauderdale Hollywood, United States",
"To": "YQB Quebec Jean Lesage, Canada",
"Op": "Air Canada Rouge",
"OpIcao": "ROU",
"Sqk": "1447",
"Help": false,
"Vsi": -2112,
"VsiT": 0,
"WTC": 2,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 1,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "1998"
}, {
"Id": 10812570,
"Rcvr": 1,
"HasSig": false,
"Icao": "A4FC9A",
"Bad": false,
"Reg": "N420LA",
"FSeen": "\/Date(1672215081608)\/",
"TSecs": 1650,
"CMsgs": 4226,
"Alt": 34000,
"GAlt": 34281,
"InHg": 30.2007866,
"AltT": 0,
"Call": "LAE2512",
"Lat": 46.00371,
"Long": -71.20912,
"PosTime": 1672216731894,
"Mlat": false,
"Tisb": false,
"Spd": 399.0,
"Trak": 210.7,
"TrkH": false,
"Type": "B763",
"Mdl": "Boeing 767 316F/W",
"Man": "Boeing",
"CNum": "34627",
"Op": "MAS Air Cargo",
"OpIcao": "MAA",
"Sqk": "0706",
"Help": false,
"Vsi": 0,
"VsiT": 0,
"WTC": 3,
"Species": 1,
"Engines": "2",
"EngType": 3,
"EngMount": 0,
"Mil": false,
"Cou": "United States",
"HasPic": false,
"Interested": false,
"FlightsCount": 1,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 2,
"Year": "2006"
}, {
"Id": 12706028,
"Rcvr": 1,
"HasSig": false,
"Icao": "C1E0EC",
"Bad": false,
"FSeen": "\/Date(1672196473179)\/",
"TSecs": 20258,
"CMsgs": 1605,
"Alt": 125300,
"GAlt": 125300,
"AltT": 0,
"Call": "TEST1234",
"Tisb": false,
"TrkH": false,
"Sqk": "",
"VsiT": 0,
"WTC": 0,
"Species": 0,
"EngType": 0,
"EngMount": 0,
"Mil": false,
"Cou": "Canada",
"HasPic": false,
"Interested": false,
"FlightsCount": 4,
"Gnd": false,
"SpdTyp": 0,
"CallSus": false,
"Trt": 1
}],
"totalAc": 5,
"lastDv": "638077811342841157",
"shtTrlSec": 30,
"stm": 1672216731909
}
I am trying to loop and have the data for every loop in the nesty structure
As depending on the plane info, data is often changed fopr more or less inside nesty loop
I have tried everything on my low knowledge of coldfusion
Need some help and many thanks to all

Save it as a variable, then deserialize.
<cfset JSONObject = DeserializeJSON(YourVariable)>
You can then call specific key-values from it like #JSONObject.flgH#.
If you have multiple to loop through you can output it with array notation, like #JSONObject.acList.Reg[1]# - you would replace the number with a variable like [i] if you're looping through it dynamically.

You can start with:
<cfset local.stData = deserializeJSON(whatever that variable is holding the JSON) />
<!--- Now, local.stData.acList is what is called "an array of structs" --->
<cfloop array="#local.stData.acList#" index="local.n" item="local.stPlane">
<!--- In here, you can work with local.stPlane.id, for example --->
<!--- If you need to loop over the "keys" of the struct, like "Id", "Rcvr", you can do that, too. I prefer structKeyList --->
<cfloop list="#structKeyList(local.stPlane)#" index="local.sKey">
<cfif local.sKey = 'Id'>
<cfelseif local.sKey = 'Rcvr'>
</cfif>
<!--- Or... --->
<cfswitch expression="#local.sKey#">
<cfcase value="Id">
</cfcase>
<cfcase value="Rcvr">
</cfcase>
<cfdefaultcase>
<!--- For unexpected keys, or just skip this block --->
</cfdefaultcase>
</cfswitch>
</cfloop>
</cfloop>

Related

Nested Json data to postgresql using python

How can i import my json file to a postgresql using python my data will look likes
{
"blocked_by": false,
"blocking": false,
"contributors_enabled": false,
"created_at": "Thu Dec 17 06:32:35 +0000 2020",
"default_profile": true,
"default_profile_image": false,
"description": "seo",
"entities": {
"description": {
"urls": []
}
},
"favourites_count": 12,
"follow_request_sent": false,
"followers_count": 56,
"following": false,
"friends_count": 1344,
"geo_enabled": false,
"has_extended_profile": true,
"id": 1339458394508374018,
"id_str": "1339458394508374018",
"is_translation_enabled": false,
"is_translator": false,
"lang": null,
"listed_count": 3,
"live_following": false,
"location": "",
"muting": false,
"name": "katheryn myle",
"notifications": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/1343124937389842432/30cfUmGe_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1343124937389842432/30cfUmGe_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"protected": false,
"screen_name": "KatherynMyle",
"status": {
"contributors": null,
"coordinates": null,
"created_at": "Tue Apr 05 10:17:04 +0000 2022",
"entities": {
"hashtags": [],
"symbols": [],
"urls": [
{
"display_url": "twitter.com/i/web/status/1\u2026",
"expanded_url": "https://twitter.com/i/web/status/1511286791319564293",
"indices": [
117,
140
],
"url": "shortened url"
}
],
"user_mentions": [
{
"id": 2247373052,
"id_str": "2247373052",
"indices": [
0,
12
],
"name": "GDevelop",
"screen_name": "GDevelopApp"
}
]
},
"favorite_count": 0,
"favorited": false,
"geo": null,
"id": 1511286791319564293,
"id_str": "1511286791319564293",
"in_reply_to_screen_name": "GDevelopApp",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": 2247373052,
"in_reply_to_user_id_str": "2247373052",
"is_quote_status": false,
"lang": "en",
"place": null,
"retweet_count": 0,
"retweeted": false,
"source": "Twitter Web App",
"text": "#GDevelopApp Hello\nHow are you fine I am a link builder and we are selling links and posts on high-quality sites if\u2026 ",
"truncated": true
},
"statuses_count": 382,
"time_zone": null,
"translator_type": "none",
"url": null,
"utc_offset": null,
"verified": false,
"withheld_in_countries": []
I want to create a table with it have ing the index or key name as the column name and also i want only some index data from it not all (like utc _offset,place,etc.).so i want to insert the specific data i wanted to postgresql
https://drive.google.com/file/d/1OkJFfHyU4Eb-V3mrU2qJ_jwBaQQkunJa/view?usp=drivesdk this is the json file
Your question is very vague and I can't come up with a short answer except this one:
pSQL does support JSON as a data type:
https://www.postgresql.org/docs/current/datatype-json.html
https://www.postgresql.org/docs/current/functions-json.html
You could just store the whole json as a single json element and select/convert/whatever within postgre later on.
How to insert JSONB into Postgresql with Python?

Vega does not correctly read timestamps from python `time.time()`

I have the following vega chart:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"name": "data",
"values": [
{
"action_hidden_size": 128,
"async_envs": true,
"charts_path": "charts/sweep",
"entropy_coef": 0.01,
"env": "BreakoutNoFrameskip-v0",
"episode length": 168.8265306122449,
"episode return": 1.0714285714285714,
"epsilon": 0.0001,
"fps": 427.7912197785782,
"gamma": 0.99,
"gpt": "1558M",
"gradient norm": 0.04117301106452942,
"jit": true,
"lambda_": 1,
"learning_rate": 0.0009,
"log_interval": 100,
"log_level": "INFO",
"logger": "hasura",
"loss": -0.028508108109235764,
"num_envs": 16,
"num_steps": 5,
"resnet": false,
"rnn": false,
"run ID": 1459,
"seed": 0,
"step": 64000,
"subcommand": "sweep",
"sweep_id": 19,
"time": 1625243790.698869,
"training_steps": -1
},
{
"action_hidden_size": 64,
"async_envs": true,
"charts_path": "charts/sweep",
"entropy_coef": 0.01,
"env": "BreakoutNoFrameskip-v0",
"episode length": 178.2111111111111,
"episode return": 1.288888888888889,
"epsilon": 0.00001,
"fps": 437.3014892700601,
"gamma": 0.99,
"gpt": "1558M",
"gradient norm": 0.0920964702963829,
"jit": true,
"lambda_": 1,
"learning_rate": 0.0005,
"log_interval": 100,
"log_level": "INFO",
"logger": "hasura",
"loss": 0.008676287718117237,
"num_envs": 16,
"num_steps": 10,
"resnet": false,
"rnn": false,
"run ID": 1456,
"seed": 0,
"step": 64000,
"subcommand": "sweep",
"sweep_id": 23,
"time": 1625243788.6341043,
"training_steps": -1
},
{
"action_hidden_size": 64,
"async_envs": true,
"charts_path": "charts/sweep",
"entropy_coef": 0.01,
"env": "BreakoutNoFrameskip-v0",
"episode length": 178.75581395348837,
"episode return": 1.430232558139535,
"epsilon": 0.00001,
"fps": 462.03207466360925,
"gamma": 0.99,
"gpt": "1558M",
"gradient norm": 0.041478127241134644,
"jit": true,
"lambda_": 1,
"learning_rate": 0.0005,
"log_interval": 100,
"log_level": "INFO",
"logger": "hasura",
"loss": 0.0075914873741567135,
"num_envs": 16,
"num_steps": 10,
"resnet": false,
"rnn": false,
"run ID": 1452,
"seed": 0,
"step": 64000,
"subcommand": "sweep",
"sweep_id": 23,
"time": 1625243776.3521159,
"training_steps": -1
},
{
"action_hidden_size": 128,
"async_envs": true,
"charts_path": "charts/sweep",
"entropy_coef": 0.01,
"env": "BreakoutNoFrameskip-v0",
"episode length": 161.39583333333334,
"episode return": 0.9166666666666666,
"epsilon": 0.0001,
"fps": 271.2519726477831,
"gamma": 0.99,
"gpt": "1558M",
"gradient norm": 0.0941310003399849,
"jit": true,
"lambda_": 1,
"learning_rate": 0.0009,
"log_interval": 100,
"log_level": "INFO",
"logger": "hasura",
"loss": -0.025099074468016624,
"num_envs": 16,
"num_steps": 5,
"resnet": false,
"rnn": false,
"run ID": 1455,
"seed": 0,
"step": 32000,
"subcommand": "sweep",
"sweep_id": 19,
"time": 1625243760.262077,
"training_steps": -1
},
{
"action_hidden_size": 128,
"async_envs": true,
"charts_path": "charts/sweep",
"entropy_coef": 0.01,
"env": "BreakoutNoFrameskip-v0",
"episode length": 156.8181818181818,
"episode return": 0.8863636363636364,
"epsilon": 0.0001,
"fps": 279.50948832177323,
"gamma": 0.99,
"gpt": "1558M",
"gradient norm": 0.06209081411361694,
"jit": true,
"lambda_": 1,
"learning_rate": 0.0009,
"log_interval": 100,
"log_level": "INFO",
"logger": "hasura",
"loss": -0.02385426126420498,
"num_envs": 16,
"num_steps": 5,
"resnet": false,
"rnn": false,
"run ID": 1459,
"seed": 0,
"step": 32000,
"subcommand": "sweep",
"sweep_id": 19,
"time": 1625243755.579455,
"training_steps": -1
}
]
},
"encoding": {
"x": {
"type": "temporal",
"field": "time"
},
"y": {
"type": "quantitative",
"field": "episode return"
},
"color": {
"type": "nominal",
"field": "run ID"
}
},
"height": 400,
"mark": "line",
"width": 600
}
Here is a link to a vega editor with this chart.
As you can see, the time-stamps do not appear to be rendering correctly. The "time" fields are derived from python's time.time() method. Should I be using a different format?
According to the documentation, vega should accept a "a timestamp number (e.g., 1552199579097)` for "temporal" fields.
I've also looked at this issue which seems to indicate that this should work in the latest Vega, although per that issue, I am not sure if I need a field along the lines of
"format": {
"parse": {
"date": "number"
}
}
Thank you.
Python's time.time() returns a timestamp in seconds since the zero epoch.
Javascript timestamps are expected to be in milliseconds since the zero epoch.
So, to correctly use Python timestamps in vega-lite charts, you'll have to multiply them by 10^6:
"transform": [
{"calculate": "1000000 * datum.time", "as": "time"}
],

Cannot read JSON in pandas

I have a nested JSON. I want it to be read in pandas in order to explore it, but I got errors. When to use read_json method, I got: "Trailing data". It is valid JSON. How to read it in pd? (Tried differently, but did not work). It looks like this:
{
"contributors": null,
"coordinates": null,
"created_at": "Fri May 26 08:54:00 +0000 2017",
"entities": {
"hashtags": [],
"media": [
{
"display_url": "pic.twitter.com/Pm28ORTePl",
"expanded_url": "",
"id": 868027417121751040,
"id_str": "868027417121751040",
"indices": [
94,
117
],
"media_url": "",
"sizes": {
"large": {
"h": 404,
"resize": "fit",
"w": 773
},
"medium": {
"h": 404,
"resize": "fit",
"w": 773
},
"small": {
"h": 355,
"resize": "fit",
"w": 680
},
"thumb": {
"h": 150,
"resize": "crop",
"w": 150
}
},
"type": "photo",
"url": ""
}
],
"symbols": [],
"urls": [
{
"display_url": "",
"expanded_url": "",
"indices": [
70,
93
],
"url": ""
}
],
"user_mentions": []
},
"extended_entities": {
"media": [
{
"display_url": "pic.twitter.com/Pm28ORTePl",
"expanded_url": "1",
"id": 868027417121751040,
"id_str": "868027417121751040",
"indices": [
94,
117
],
"media_url": "",
"media_url_https": "",
"sizes": {
"large": {
"h": 404,
"resize": "fit",
"w": 773
},
"medium": {
"h": 404,
"resize": "fit",
"w": 773
},
"small": {
"h": 355,
"resize": "fit",
"w": 680
},
"thumb": {
"h": 150,
"resize": "crop",
"w": 150
}
},
"type": "photo",
"url": ""
}
]
},
"favorite_count": 1,
"favorited": false,
"geo": null,
"id": 868027425757724672,
"id_str": "868027425757724672",
"in_reply_to_screen_name": null,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"is_quote_status": false,
"lang": "ru",
"place": null,
"possibly_sensitive": false,
"retweet_count": 0,
"retweeted": false,
"source": "Twitter Web Client",
"text": "\u041f\u0440\u043e\u043f\u0430\u0432\u0448\u0430\u044f \u0432 \u041a\u043e\u043a\u0448\u0435\u0442\u0430\u0443 \u0448\u043a\u043e\u043b\u044c\u043d\u0438\u0446\u0430 \u0436\u0438\u043b\u0430 \u0432 \u0437\u0430\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u043e\u043c \u0434\u043e\u043c\u0435 \u0438 \u0431\u0440\u043e\u0434\u044f\u0436\u043d\u0438\u0447\u0430\u043b\u0430\n",
"truncated": false,
"user": {
"contributors_enabled": false,
"created_at": "Wed May 18 11:59:50 +0000 2011",
"default_profile": true,
"default_profile_image": false,
"description": "\u041a\u0430\u0437\u0430\u0445\u0441\u0442\u0430\u043d\u0441\u043a\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442-\u043f\u043e\u0440\u0442\u0430\u043b",
"entities": {
"description": {
"urls": []
},
"url": {
"urls": [
{
"display_url": "",
"expanded_url": "",
"indices": [
0,
22
],
"url": ""
}
]
}
},
"favourites_count": 87,
"follow_request_sent": false,
"followers_count": 17989,
"following": true,
"friends_count": 98,
"geo_enabled": true,
"has_extended_profile": false,
"id": 300811189,
"id_str": "300811189",
"is_translation_enabled": false,
"is_translator": false,
"lang": "ru",
"listed_count": 86,
"location": "\u0410\u043b\u043c\u0430\u0442\u044b",
"name": "",
"notifications": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_tile": false,
"profile_banner_url": "https://pbs.twimg.com/profile_banners/300811189/1489117916",
"profile_image_url": "http://pbs.twimg.com/profile_images/840047424882298881/NxZSyfhM_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/840047424882298881/NxZSyfhM_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"protected": false,
"screen_name": "",
"statuses_count": 53011,
"time_zone": "Quito",
"translator_type": "none",
"url": "",
"utc_offset": -18000,
"verified": false
}
}
Sorry, but your JSON is actually not valid, despite your saying it is.
This line:
"media_url": "": "",
Should probably be:
"media_url": "",
At which point, when I added the final bracket } that was outside of your code block, validated as properly formed JSON.

Query Json data saved in Postgres

I'm trying to query JSON data saved in postgres.
This is how the table is created
CREATE TABLE ALARMDATA2(ALARM CHAR(1300))
This is the JSON object:
{"delay_max": 0.0, "ts_errors": [{"count": 0, "state": 0, "is_priority1": true, "name": "SYNC", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "BYTE", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PAT", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "CC", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PMT", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": true, "name": "PID", "is_priority2": false}, {"count": 0, "state": 0, "is_priority1": false, "name": "TS", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "CRC", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "PCR", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "ACC", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "PTS", "is_priority2": true}, {"count": 0, "state": 0, "is_priority1": false, "name": "CAT", "is_priority2": true}], "is_stream_paused": false, "delay_min": 0.0, "ac_err": 0.0, "oj_err": 0.0}
I'd like to query based on "delay_max" (the first entry in JSON). I am using this query
SELECT ALARM->>'delay_max' AS delay_max FROM alarmdata2;
I think the query syntax is fine as per these links (here and here) but I am getting this error
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
I've been searching for a while but I have not clue.
Any suggestions why ?
You must define the ALARM colum as type JSON or JSONB:
CREATE TABLE ALARMDATA2(ALARM JSONB)
And then it will work.
Fix the problem
ALTER TABLE alarmdata2
ALTER COLUMN alarm
TYPE jsonb
USING alarm::jsonb;
Or, work around it
SELECT ALARM::jsonb->>'delay_max' AS delay_max
FROM alarmdata2;

How to get the particular value from parsed json array by using golang?

I am trying to get direct message from twitter using twitter api. I got the api json array respose like
[{
"id": 694476444991229955,
"id_str": "694476444991229955",
"text": "Got it",
"sender": {
"id": 1690262984,
"id_str": "1690262984",
"name": "Ashok Kumar T",
"screen_name": "Ashok_kumar_T",
"location": "Trivandrum",
"description": "",
"url": null
},
"protected": false,
"followers_count": 68,
"friends_count": 32,
"listed_count": 0,
"created_at": "Thu Aug 22 06:52:53 +0000 2013",
"favourites_count": 5,
"utc_offset": 19800,
"time_zone": "Chennai",
"geo_enabled": true,
"verified": false,
"statuses_count": 124,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252",
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
}]
I have successfully parsed the json.But I cant get the particular data from parsed json. Here my play golang link http://play.golang.org/p/zS42Qws2Di
package main
import (
"encoding/json"
"fmt"
)
type PublicKey struct {
ID int64
ID_STR string
Text string
SENDER struct {
ID int64
ID_STR string
NAME string
}
PROTECTED bool
FOLLOWERS_COUNT int
FRIENDS_COUNT int
LISTED_COUNT int
}
type KeysResponse struct {
Collection []PublicKey
}
func main() {
s := `[{
"id": 694476444991229955,
"id_str": "694476444991229955",
"text": "Got it",
"sender": {
"id": 1690262984,
"id_str": "1690262984",
"name": "Ashok Kumar T",
"screen_name": "Ashok_kumar_T",
"location": "Trivandrum",
"description": "",
"url": null
},
"protected": false,
"followers_count": 68,
"friends_count": 32,
"listed_count": 0,
"created_at": "Thu Aug 22 06:52:53 +0000 2013",
"favourites_count": 5,
"utc_offset": 19800,
"time_zone": "Chennai",
"geo_enabled": true,
"verified": false,
"statuses_count": 124,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252",
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
},{
"id": 694476444991229955,
"id_str": "694476444991229955",
"text": "Got it",
"sender": {
"id": 1690262984,
"id_str": "1690262984",
"name": "Ashok Kumar T",
"screen_name": "Ashok_kumar_T",
"location": "Trivandrum",
"description": "",
"url": null
},
"protected": false,
"followers_count": 68,
"friends_count": 32,
"listed_count": 0,
"created_at": "Thu Aug 22 06:52:53 +0000 2013",
"favourites_count": 5,
"utc_offset": 19800,
"time_zone": "Chennai",
"geo_enabled": true,
"verified": false,
"statuses_count": 124,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252",
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
}]`
keys := make([]PublicKey,0)
err := json.Unmarshal([]byte(s), &keys)
if err == nil {
fmt.Printf("%+v\n", keys)
} else {
fmt.Println(err)
fmt.Printf("%+v\n", keys)
}
myId := keys[l].Id
fmt.Printf(myId)
}
If the goal is to print the identifier of the second array element, then use this code:
myId := keys[1].ID // l changed 1, Id changed to ID
fmt.Println(myId)
playground example