JSON, Big HARD string.

Just starting out? Need help? Post your questions and find answers here.
registrymechanic
New User
New User
Posts: 5
Joined: Fri Apr 16, 2021 11:29 am

JSON, Big HARD string.

Post by registrymechanic »

Hello.

initial data in this form:

Code: Select all

 {"response":{"count":6391,"items":[{"id":27573,"from_id":-70345823,"owner_id":-70345823,"date":1618251595,"marked_as_ads":0,"post_type":"post","text":"[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk","is_pinned":1,"attachments":[{"type":"photo","photo":{"album_id":-7,"date":1618251595,"id":457285654,"owner_id":-70384223,"has_tags":false,"access_key":"6703f0e348e6eb9871","sizes":[{"height":130,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album","type":"m","width":130},{"height":130,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album","type":"o","width":130},{"height":200,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album","type":"p","width":200},{"height":320,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album","type":"q","width":320},{"height":510,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album","type":"r","width":510},{"height":75,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album","type":"s","width":75},{"height":604,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album","type":"x","width":604},{"height":807,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album","type":"y","width":807},{"height":1080,"url":"https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album","type":"z","width":1080}],"text":"","user_id":100}},{"type":"audio","audio":{"artist":"Deep Purple","id":456244830,"owner_id":2000217972,"title":"Smoke on the Water","duration":25,"is_explicit":false,"is_focus_track":false,"track_code":"e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0","url":"https:\/\/vk.com\/mp3\/audio_api_unavailable.mp3","date":1618251595,"main_artists":[{"name":"Deep Purple","domain":"1798617373489613151","id":"1798617373489613151"}],"subtitle":"2002 Remaster","short_videos_allowed":false,"stories_allowed":false,"stories_cover_allowed":false}}],"post_source":{"type":"api","platform":"iphone"},"comments":{"count":0,"can_post":1,"groups_can_post":true},"likes":{"count":33,"user_likes":0,"can_like":1,"can_publish":1},"reposts":{"count":24,"user_reposted":0},"views":{"count":4286},"is_favorite":false,"donut":{"is_donut":false},"short_text_rate":0.800000,"carousel_offset":0,"edited":1618252199}]}}


I can't figure out how to parse it using JSON.
Explain, please.
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: JSON, Big HARD string.

Post by devox »

Code: Select all

#JSONID = 0
Global JsonData$ = " {" + Chr($22) + "response" + Chr($22) + ":{" + Chr($22) + "count" + Chr($22) + ":6391," + Chr($22) + "items" + Chr($22) + ":[{" + Chr($22) + "id" + Chr($22) + ":27573," + Chr($22) + "from_id" + Chr($22) + ":-70345823," + Chr($22) + "owner_id" + Chr($22) + ":-70345823," + Chr($22) + "date" + Chr($22) + ":1618251595," + Chr($22) + "marked_as_ads" + Chr($22) + ":0," + Chr($22) + "post_type" + Chr($22) + ":" + Chr($22) + "post" + Chr($22) + "," + Chr($22) + "text" + Chr($22) + ":" + Chr($22) + "[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk" + Chr($22) + "," + Chr($22) + "is_pinned" + Chr($22) + ":1," + Chr($22) + "attachments" + Chr($22) + ":[{" + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "photo" + Chr($22) + "," + Chr($22) + "photo" + Chr($22) + ":{" + Chr($22) + "album_id" + Chr($22) + ":-7," + Chr($22) + "date" + Chr($22) + ":1618251595," + Chr($22) + "id" + Chr($22) + ":457285654," + Chr($22) + "owner_id" + Chr($22) + ":-70384223," + Chr($22) + "has_tags" + Chr($22) + ":false," + Chr($22) + "access_key" + Chr($22) + ":" + Chr($22) + "6703f0e348e6eb9871" + Chr($22) + "," + Chr($22) + "sizes" + Chr($22) + ":[{" + Chr($22) + "height" + Chr($22) + ":130," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "m" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":130},{" + Chr($22) + "height" + Chr($22) + ":130," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "o" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":130},{" + Chr($22) + "height" + Chr($22) + ":200," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "p" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":200},{" + Chr($22) + "height" + Chr($22) + ":320," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "q" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":320},{" + Chr($22) + "height" + Chr($22) + ":510," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "r" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":510},{" + Chr($22) + "height" + Chr($22) + ":75," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "s" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":75},{" + Chr($22) + "height" + Chr($22) + ":604," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "x" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":604},{" + Chr($22) + "height" + Chr($22) + ":807," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "y" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":807},{" + Chr($22) + "height" + Chr($22) + ":1080," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album" + Chr($22) + "," + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "z" + Chr($22) + "," + Chr($22) + "width" + Chr($22) + ":1080}]," + Chr($22) + "text" + Chr($22) + ":" + Chr($22) + "" + Chr($22) + "," + Chr($22) + "user_id" + Chr($22) + ":100}},{" + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "audio" + Chr($22) + "," + Chr($22) + "audio" + Chr($22) + ":{" + Chr($22) + "artist" + Chr($22) + ":" + Chr($22) + "Deep Purple" + Chr($22) + "," + Chr($22) + "id" + Chr($22) + ":456244830," + Chr($22) + "owner_id" + Chr($22) + ":2000217972," + Chr($22) + "title" + Chr($22) + ":" + Chr($22) + "Smoke on the Water" + Chr($22) + "," + Chr($22) + "duration" + Chr($22) + ":25," + Chr($22) + "is_explicit" + Chr($22) + ":false," + Chr($22) + "is_focus_track" + Chr($22) + ":false," + Chr($22) + "track_code" + Chr($22) + ":" + Chr($22) + "e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0" + Chr($22) + "," + Chr($22) + "url" + Chr($22) + ":" + Chr($22) + "https:\/\/vk.com\/mp3\/audio_api_unavailable.mp3" + Chr($22) + "," + Chr($22) + "date" + Chr($22) + ":1618251595," + Chr($22) + "main_artists" + Chr($22) + ":[{" + Chr($22) + "name" + Chr($22) + ":" + Chr($22) + "Deep Purple" + Chr($22) + "," + Chr($22) + "domain" + Chr($22) + ":" + Chr($22) + "1798617373489613151" + Chr($22) + "," + Chr($22) + "id" + Chr($22) + ":" + Chr($22) + "1798617373489613151" + Chr($22) + "}]," + Chr($22) + "subtitle" + Chr($22) + ":" + Chr($22) + "2002 Remaster" + Chr($22) + "," + Chr($22) + "short_videos_allowed" + Chr($22) + ":false," + Chr($22) + "stories_allowed" + Chr($22) + ":false," + Chr($22) + "stories_cover_allowed" + Chr($22) + ":false}}]," + Chr($22) + "post_source" + Chr($22) + ":{" + Chr($22) + "type" + Chr($22) + ":" + Chr($22) + "api" + Chr($22) + "," + Chr($22) + "platform" + Chr($22) + ":" + Chr($22) + "iphone" + Chr($22) + "}," + Chr($22) + "comments" + Chr($22) + ":{" + Chr($22) + "count" + Chr($22) + ":0," + Chr($22) + "can_post" + Chr($22) + ":1," + Chr($22) + "groups_can_post" + Chr($22) + ":true}," + Chr($22) + "likes" + Chr($22) + ":{" + Chr($22) + "count" + Chr($22) + ":33," + Chr($22) + "user_likes" + Chr($22) + ":0," + Chr($22) + "can_like" + Chr($22) + ":1," + Chr($22) + "can_publish" + Chr($22) + ":1}," + Chr($22) + "reposts" + Chr($22) + ":{" + Chr($22) + "count" + Chr($22) + ":24," + Chr($22) + "user_reposted" + Chr($22) + ":0}," + Chr($22) + "views" + Chr($22) + ":{" + Chr($22) + "count" + Chr($22) + ":4286}," + Chr($22) + "is_favorite" + Chr($22) + ":false," + Chr($22) + "donut" + Chr($22) + ":{" + Chr($22) + "is_donut" + Chr($22) + ":false}," + Chr($22) + "short_text_rate" + Chr($22) + ":0.800000," + Chr($22) + "carousel_offset" + Chr($22) + ":0," + Chr($22) + "edited" + Chr($22) + ":1618252199}]}}" + #CRLF$

If ParseJSON(#JSONID, JsonData$)
    MessageRequester("Parsed", "Json Data parsed")
EndIf

Inside of the if you can then do normal JSON operations for navigating the data or even extract the json to a structure.
registrymechanic
New User
New User
Posts: 5
Joined: Fri Apr 16, 2021 11:29 am

Re: JSON, Big HARD string.

Post by registrymechanic »

but how to read data from all this?

Code: Select all

Debug response
Debug count
?


or

Code: Select all

    For i = 0 To JSONArraySize(JSONValue(#JSONID)) - 1
      Debug GetJSONInteger(GetJSONElement(JSONValue(#JSONID), i))
    Next   

?
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: JSON, Big HARD string.

Post by IdeasVacuum »

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
registrymechanic
New User
New User
Posts: 5
Joined: Fri Apr 16, 2021 11:29 am

Re: JSON, Big HARD string.

Post by registrymechanic »

IdeasVacuum wrote: Fri Apr 16, 2021 8:34 pm This will help a bit:
https://www.w3schools.com/js/js_json_intro.asp
Unfortunately, I don't understand this, I need an example to understand the algorithm... (((
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: JSON, Big HARD string.

Post by NicTheQuick »

First do you a favor and make a pretty print to understand the structure:

Code: Select all

json.s = ~" {\"response\":{\"count\":6391,\"items\":[{\"id\":27573,\"from_id\":-70345823,\"owner_id\":-70345823,\"date\":1618251595,\"marked_as_ads\":0,\"post_type\":\"post\",\"text\":\"[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk\",\"is_pinned\":1,\"attachments\":[{\"type\":\"photo\",\"photo\":{\"album_id\":-7,\"date\":1618251595,\"id\":457285654,\"owner_id\":-70384223,\"has_tags\":false,\"access_key\":\"6703f0e348e6eb9871\",\"sizes\":[{\"height\":130,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album\",\"type\":\"m\",\"width\":130},{\"height\":130,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album\",\"type\":\"o\",\"width\":130},{\"height\":200,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album\",\"type\":\"p\",\"width\":200},{\"height\":320,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album\",\"type\":\"q\",\"width\":320},{\"height\":510,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album\",\"type\":\"r\",\"width\":510},{\"height\":75,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album\",\"type\":\"s\",\"width\":75},{\"height\":604,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album\",\"type\":\"x\",\"width\":604},{\"height\":807,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album\",\"type\":\"y\",\"width\":807},{\"height\":1080,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album\",\"type\":\"z\",\"width\":1080}],\"text\":\"\",\"user_id\":100}},{\"type\":\"audio\",\"audio\":{\"artist\":\"Deep Purple\",\"id\":456244830,\"owner_id\":2000217972,\"title\":\"Smoke on the Water\",\"duration\":25,\"is_explicit\":false,\"is_focus_track\":false,\"track_code\":\"e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0\",\"url\":\"https://vk.com/mp3/audio_api_unavailable.mp3\",\"date\":1618251595,\"main_artists\":[{\"name\":\"Deep Purple\",\"domain\":\"1798617373489613151\",\"id\":\"1798617373489613151\"}],\"subtitle\":\"2002 Remaster\",\"short_videos_allowed\":false,\"stories_allowed\":false,\"stories_cover_allowed\":false}}],\"post_source\":{\"type\":\"api\",\"platform\":\"iphone\"},\"comments\":{\"count\":0,\"can_post\":1,\"groups_can_post\":true},\"likes\":{\"count\":33,\"user_likes\":0,\"can_like\":1,\"can_publish\":1},\"reposts\":{\"count\":24,\"user_reposted\":0},\"views\":{\"count\":4286},\"is_favorite\":false,\"donut\":{\"is_donut\":false},\"short_text_rate\":0.800000,\"carousel_offset\":0,\"edited\":1618252199}]}}"

If ParseJSON(0, json)
	Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
Then use ExtractJSONStructure to actually load it into structured variables.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: JSON, Big HARD string.

Post by NicTheQuick »

I started the code for you. You have to implement the structures "photo" and "audio" by yourself.

Code: Select all

json.s = ~" {\"response\":{\"count\":6391,\"items\":[{\"id\":27573,\"from_id\":-70345823,\"owner_id\":-70345823,\"date\":1618251595,\"marked_as_ads\":0,\"post_type\":\"post\",\"text\":\"[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk\",\"is_pinned\":1,\"attachments\":[{\"type\":\"photo\",\"photo\":{\"album_id\":-7,\"date\":1618251595,\"id\":457285654,\"owner_id\":-70384223,\"has_tags\":false,\"access_key\":\"6703f0e348e6eb9871\",\"sizes\":[{\"height\":130,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album\",\"type\":\"m\",\"width\":130},{\"height\":130,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album\",\"type\":\"o\",\"width\":130},{\"height\":200,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album\",\"type\":\"p\",\"width\":200},{\"height\":320,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album\",\"type\":\"q\",\"width\":320},{\"height\":510,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album\",\"type\":\"r\",\"width\":510},{\"height\":75,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album\",\"type\":\"s\",\"width\":75},{\"height\":604,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album\",\"type\":\"x\",\"width\":604},{\"height\":807,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album\",\"type\":\"y\",\"width\":807},{\"height\":1080,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album\",\"type\":\"z\",\"width\":1080}],\"text\":\"\",\"user_id\":100}},{\"type\":\"audio\",\"audio\":{\"artist\":\"Deep Purple\",\"id\":456244830,\"owner_id\":2000217972,\"title\":\"Smoke on the Water\",\"duration\":25,\"is_explicit\":false,\"is_focus_track\":false,\"track_code\":\"e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0\",\"url\":\"https://vk.com/mp3/audio_api_unavailable.mp3\",\"date\":1618251595,\"main_artists\":[{\"name\":\"Deep Purple\",\"domain\":\"1798617373489613151\",\"id\":\"1798617373489613151\"}],\"subtitle\":\"2002 Remaster\",\"short_videos_allowed\":false,\"stories_allowed\":false,\"stories_cover_allowed\":false}}],\"post_source\":{\"type\":\"api\",\"platform\":\"iphone\"},\"comments\":{\"count\":0,\"can_post\":1,\"groups_can_post\":true},\"likes\":{\"count\":33,\"user_likes\":0,\"can_like\":1,\"can_publish\":1},\"reposts\":{\"count\":24,\"user_reposted\":0},\"views\":{\"count\":4286},\"is_favorite\":false,\"donut\":{\"is_donut\":false},\"short_text_rate\":0.800000,\"carousel_offset\":0,\"edited\":1618252199}]}}"

Structure resposts
	user_reposted.i
	count.i
EndStructure

Structure comments
	can_post.i
	count.i
	groups_can_post.i ;bool
EndStructure

Structure post_source
	platform.s
	type.s
EndStructure

Structure donut
	is_donut.i ;bool
EndStructure

Structure views
	count.i
EndStructure

Structure photo
EndStructure
Structure audio
EndStructure

Structure attachment
	StructureUnion
		photo.photo
		audio.audio
	EndStructureUnion
	type.s
EndStructure

Structure likes
	can_publish.i
	can_like.i
	user_likes.i
	count.i
EndStructure

Structure item
	reposts.resposts
	edited.i
	is_pinned.i
	from_id.i
	comments.comments
	marked_as_ads.i
	post_type.s
	post_source.post_source
	donut.donut
	owner_id.i
	text.s
	views.views
	date.i
	short_text_rate.f
	Array attachments.attachment(0)
	is_favorite.i ;bool
	carousel_offset.i
	id.i
	likes.likes
EndStructure

Structure response
	Array items.item(0)
	count.i
EndStructure

Structure request
	response.response
EndStructure

Define request.request

If ParseJSON(0, json)
	ExtractJSONStructure(JSONValue(0), @request, request)
	
	Debug request\response\items(0)\text
EndIf
Last edited by NicTheQuick on Fri Apr 16, 2021 9:28 pm, edited 1 time in total.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

Re: JSON, Big HARD string.

Post by fluent »

A very easy way.
Look at the example at the bottom. (to make it easier to post the example on the forum I replaced the quotes in a text editor)

Code: Select all

;;  MODULE by LITTLE JOHN

DeclareModule JSON
  Declare.s Get (jn.i, path$, quote$="'")
EndDeclareModule

Module JSON
  EnableExplicit
  
  Procedure.i _GetJSONValue (jn.i, path$)
    ; -- internal function
    Protected jv.i, depth.i, level.i, index.i, field$
    
    jv = JSONValue(jn)
    
    If jv <> 0 And path$ <> ""
      depth = CountString(path$, "\") + 1
      For level = 1 To depth
        field$ = StringField(path$, level, "\")
        If Left(field$, 1) = "[" And Right(field$, 1) = "]"
          index = Val(Mid(field$, 2, Len(field$)-2))
          If JSONType(jv) = #PB_JSON_Array And index >= 0 And index < JSONArraySize(jv)
            jv = GetJSONElement(jv, index)
          Else
            jv = 0
          EndIf
        Else   
          If JSONType(jv) = #PB_JSON_Object
            jv = GetJSONMember(jv, field$)   ; 0 if the given 'field$' does not exist in the object
          Else
            jv = 0
          EndIf
        EndIf
      Next   
    EndIf
    
    ProcedureReturn jv
  EndProcedure
  
  
  Procedure.s Get (jn.i, path$, quote$="'")
    ; in : jn    : JSON number, e.g. generated by ParseJSON()
    ;      path$ : path to the JSON element that is to be retrieved
    ;      quote$: character that is regarded as quote
    ; out: value of the desired element
    Protected jv.i, ret$=""
    
    If jn
      jv = _GetJSONValue(jn, path$)
      If jv
        Select JSONType(jv)
          Case #PB_JSON_String 
            ret$ = quote$ + GetJSONString(jv) + quote$
          Case #PB_JSON_Boolean 
            If GetJSONBoolean(jv) = #True
              ret$ = "true"
            Else
              ret$ = "false"
            EndIf   
          Case #PB_JSON_Null 
            ret$ = "null" 
          Case #PB_JSON_Number
            ret$ = StrD(GetJSONDouble(jv)) 
        EndSelect      
      EndIf
    EndIf
    
    ProcedureReturn ret$
  EndProcedure
  
EndModule

 
; ; ==== Demo ====
   
json.s = "{'response':{'count':6391,'items':[{'id':27573,'from_id':-70345823,'owner_id':-70345823,'date':1618251595,'marked_as_ads':0,'post_type':'post','text':'[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk','is_pinned':1,'attachments':[{'type':'photo','photo':{'album_id':-7,'date':1618251595,'id':457285654,'owner_id':-70384223,'has_tags':false,'access_key':'6703f0e348e6eb9871','sizes':[{'height':130,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album','type':'m','width':130},{'height':130,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album','type':'o','width':130},{'height':200,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album','type':'p','width':200},{'height':320,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album','type':'q','width':320},{'height':510,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album','type':'r','width':510},{'height':75,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album','type':'s','width':75},{'height':604,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album','type':'x','width':604},{'height':807,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album','type':'y','width':807},{'height':1080,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album','type':'z','width':1080}],'text':'','user_id':100}},{'type':'audio','audio':{'artist':'Deep Purple','id':456244830,'owner_id':2000217972,'title':'Smoke on the Water','duration':25,'is_explicit':false,'is_focus_track':false,'track_code':'e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0','url':'https:\/\/vk.com\/mp3\/audio_api_unavailable.mp3','date':1618251595,'main_artists':[{'name':'Deep Purple','domain':'1798617373489613151','id':'1798617373489613151'}],'subtitle':'2002 Remaster','short_videos_allowed':false,'stories_allowed':false,'stories_cover_allowed':false}}],'post_source':{'type':'api','platform':'iphone'},'comments':{'count':0,'can_post':1,'groups_can_post':true},'likes':{'count':33,'user_likes':0,'can_like':1,'can_publish':1},'reposts':{'count':24,'user_reposted':0},'views':{'count':4286},'is_favorite':false,'donut':{'is_donut':false},'short_text_rate':0.800000,'carousel_offset':0,'edited':1618252199}]}}"
ReplaceString(json, "'", Chr(34), #PB_String_InPlace) 

jn = ParseJSON(#PB_Any, json)

Debug "count: "     + JSON::GET(jn, "response\count") 
Debug "item 0 id: " + JSON::GET(jn, "response\items\[0]\id") 

registrymechanic
New User
New User
Posts: 5
Joined: Fri Apr 16, 2021 11:29 am

Re: JSON, Big HARD string.

Post by registrymechanic »

NicTheQuick wrote: Fri Apr 16, 2021 9:22 pm I started the code for you. You have to implement the structures "photo" and "audio" by yourself.

Code: Select all

json.s = ~" {\"response\":{\"count\":6391,\"items\":[{\"id\":27573,\"from_id\":-70345823,\"owner_id\":-70345823,\"date\":1618251595,\"marked_as_ads\":0,\"post_type\":\"post\",\"text\":\"[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk\",\"is_pinned\":1,\"attachments\":[{\"type\":\"photo\",\"photo\":{\"album_id\":-7,\"date\":1618251595,\"id\":457285654,\"owner_id\":-70384223,\"has_tags\":false,\"access_key\":\"6703f0e348e6eb9871\",\"sizes\":[{\"height\":130,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album\",\"type\":\"m\",\"width\":130},{\"height\":130,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album\",\"type\":\"o\",\"width\":130},{\"height\":200,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album\",\"type\":\"p\",\"width\":200},{\"height\":320,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album\",\"type\":\"q\",\"width\":320},{\"height\":510,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album\",\"type\":\"r\",\"width\":510},{\"height\":75,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album\",\"type\":\"s\",\"width\":75},{\"height\":604,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album\",\"type\":\"x\",\"width\":604},{\"height\":807,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album\",\"type\":\"y\",\"width\":807},{\"height\":1080,\"url\":\"https://sun9-13.userapi.com/impg/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album\",\"type\":\"z\",\"width\":1080}],\"text\":\"\",\"user_id\":100}},{\"type\":\"audio\",\"audio\":{\"artist\":\"Deep Purple\",\"id\":456244830,\"owner_id\":2000217972,\"title\":\"Smoke on the Water\",\"duration\":25,\"is_explicit\":false,\"is_focus_track\":false,\"track_code\":\"e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0\",\"url\":\"https://vk.com/mp3/audio_api_unavailable.mp3\",\"date\":1618251595,\"main_artists\":[{\"name\":\"Deep Purple\",\"domain\":\"1798617373489613151\",\"id\":\"1798617373489613151\"}],\"subtitle\":\"2002 Remaster\",\"short_videos_allowed\":false,\"stories_allowed\":false,\"stories_cover_allowed\":false}}],\"post_source\":{\"type\":\"api\",\"platform\":\"iphone\"},\"comments\":{\"count\":0,\"can_post\":1,\"groups_can_post\":true},\"likes\":{\"count\":33,\"user_likes\":0,\"can_like\":1,\"can_publish\":1},\"reposts\":{\"count\":24,\"user_reposted\":0},\"views\":{\"count\":4286},\"is_favorite\":false,\"donut\":{\"is_donut\":false},\"short_text_rate\":0.800000,\"carousel_offset\":0,\"edited\":1618252199}]}}"

Structure resposts
	user_reposted.i
	count.i
EndStructure

Structure comments
	can_post.i
	count.i
	groups_can_post.i ;bool
EndStructure

Structure post_source
	platform.s
	type.s
EndStructure

Structure donut
	is_donut.i ;bool
EndStructure

Structure views
	count.i
EndStructure

Structure photo
EndStructure
Structure audio
EndStructure

Structure attachment
	StructureUnion
		photo.photo
		audio.audio
	EndStructureUnion
	type.s
EndStructure

Structure likes
	can_publish.i
	can_like.i
	user_likes.i
	count.i
EndStructure

Structure item
	reposts.resposts
	edited.i
	is_pinned.i
	from_id.i
	comments.comments
	marked_as_ads.i
	post_type.s
	post_source.post_source
	donut.donut
	owner_id.i
	text.s
	views.views
	date.i
	short_text_rate.f
	Array attachments.attachment(0)
	is_favorite.i ;bool
	carousel_offset.i
	id.i
	likes.likes
EndStructure

Structure response
	Array items.item(0)
	count.i
EndStructure

Structure request
	response.response
EndStructure

Define request.request

If ParseJSON(0, json)
	ExtractJSONStructure(JSONValue(0), @request, request)
	
	Debug request\response\items(0)\text
EndIf
You have done a great job. Many thanks !!! This clarifies the whole point of working with JSON for me. THANK YOU!
registrymechanic
New User
New User
Posts: 5
Joined: Fri Apr 16, 2021 11:29 am

Re: JSON, Big HARD string.

Post by registrymechanic »

fluent wrote: Fri Apr 16, 2021 9:25 pm A very easy way.
Look at the example at the bottom. (to make it easier to post the example on the forum I replaced the quotes in a text editor)

Code: Select all

;;  MODULE by LITTLE JOHN

DeclareModule JSON
  Declare.s Get (jn.i, path$, quote$="'")
EndDeclareModule

Module JSON
  EnableExplicit
  
  Procedure.i _GetJSONValue (jn.i, path$)
    ; -- internal function
    Protected jv.i, depth.i, level.i, index.i, field$
    
    jv = JSONValue(jn)
    
    If jv <> 0 And path$ <> ""
      depth = CountString(path$, "\") + 1
      For level = 1 To depth
        field$ = StringField(path$, level, "\")
        If Left(field$, 1) = "[" And Right(field$, 1) = "]"
          index = Val(Mid(field$, 2, Len(field$)-2))
          If JSONType(jv) = #PB_JSON_Array And index >= 0 And index < JSONArraySize(jv)
            jv = GetJSONElement(jv, index)
          Else
            jv = 0
          EndIf
        Else   
          If JSONType(jv) = #PB_JSON_Object
            jv = GetJSONMember(jv, field$)   ; 0 if the given 'field$' does not exist in the object
          Else
            jv = 0
          EndIf
        EndIf
      Next   
    EndIf
    
    ProcedureReturn jv
  EndProcedure
  
  
  Procedure.s Get (jn.i, path$, quote$="'")
    ; in : jn    : JSON number, e.g. generated by ParseJSON()
    ;      path$ : path to the JSON element that is to be retrieved
    ;      quote$: character that is regarded as quote
    ; out: value of the desired element
    Protected jv.i, ret$=""
    
    If jn
      jv = _GetJSONValue(jn, path$)
      If jv
        Select JSONType(jv)
          Case #PB_JSON_String 
            ret$ = quote$ + GetJSONString(jv) + quote$
          Case #PB_JSON_Boolean 
            If GetJSONBoolean(jv) = #True
              ret$ = "true"
            Else
              ret$ = "false"
            EndIf   
          Case #PB_JSON_Null 
            ret$ = "null" 
          Case #PB_JSON_Number
            ret$ = StrD(GetJSONDouble(jv)) 
        EndSelect      
      EndIf
    EndIf
    
    ProcedureReturn ret$
  EndProcedure
  
EndModule

 
; ; ==== Demo ====
   
json.s = "{'response':{'count':6391,'items':[{'id':27573,'from_id':-70345823,'owner_id':-70345823,'date':1618251595,'marked_as_ads':0,'post_type':'post','text':'[club1440457|DDDDDDD «GGGGGGG»] HHHHHHHHHH\n\nkkkkkkkkkkkkkkkkkkkkkk🚀\n\nkkkkkkkkkkkkkkkkkkk','is_pinned':1,'attachments':[{'type':'photo','photo':{'album_id':-7,'date':1618251595,'id':457285654,'owner_id':-70384223,'has_tags':false,'access_key':'6703f0e348e6eb9871','sizes':[{'height':130,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album','type':'m','width':130},{'height':130,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=130x130&quality=96&sign=e3af5c2137eef675f544fb7a90847499&c_uniq_tag=W1VEWy2qtWM1z_5eYSm9Qtwx8JR7RFGAA8vmoaozLvk&type=album','type':'o','width':130},{'height':200,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=200x200&quality=96&sign=9a7b42032bdffc392c2c8fb79223fc8c&c_uniq_tag=1ngmvWUaPw6ARkmTEPQXcLWZJ8XRk71So5mcfBRKolE&type=album','type':'p','width':200},{'height':320,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=320x320&quality=96&sign=2fc7e3cdc9b7a4d2b6a0dd3519f02d4d&c_uniq_tag=DMy14rTnJ22JOuXfjnMTemTBhwYgoN2Z4i-VC59WI68&type=album','type':'q','width':320},{'height':510,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=510x510&quality=96&sign=c4711937de73d5a22a77ceadd8914f92&c_uniq_tag=IO_zhYPG7XJ55dpOeQrHMcnSflcTyT1q-J-0TdoPDDI&type=album','type':'r','width':510},{'height':75,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=75x75&quality=96&sign=40d01d8d3e42222ed154b8e2ef16d919&c_uniq_tag=MS4cVAtMREVLig4tRXD_PNudiePWZgQrMJvmVAXT66Y&type=album','type':'s','width':75},{'height':604,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=604x604&quality=96&sign=bb78889dff6e0dca12121d26170021ac&c_uniq_tag=np_Cf8t0xS83HtI5SC4J1OR_i3BpDSwNeO93tEDH8Wc&type=album','type':'x','width':604},{'height':807,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=807x807&quality=96&sign=79987dc5b41627389d9637ff8197b18c&c_uniq_tag=2we5WOqLIVf2vpKnp07_sRPsAO7dmXvWxeAfiA6SJtk&type=album','type':'y','width':807},{'height':1080,'url':'https:\/\/sun9-13.userapi.com\/impg\/xuwBNyLg3t9aC6NyOxXYuf5T9akGZa8PfyAz0A\/RVUX7ohfezw.jpg?size=1080x1080&quality=96&sign=f9cd3ca7a5d89589974fbdf0ebe97362&c_uniq_tag=B0nxv88lmbdg5EYET4A_AVJbFuGqtALBhJCfwpf_YhM&type=album','type':'z','width':1080}],'text':'','user_id':100}},{'type':'audio','audio':{'artist':'Deep Purple','id':456244830,'owner_id':2000217972,'title':'Smoke on the Water','duration':25,'is_explicit':false,'is_focus_track':false,'track_code':'e1b0cf34xGP41LjzZlkyoWOMhJ7xCfR3WPnWasMJYMPzFWUPa_0hjBqmzJIFMW0','url':'https:\/\/vk.com\/mp3\/audio_api_unavailable.mp3','date':1618251595,'main_artists':[{'name':'Deep Purple','domain':'1798617373489613151','id':'1798617373489613151'}],'subtitle':'2002 Remaster','short_videos_allowed':false,'stories_allowed':false,'stories_cover_allowed':false}}],'post_source':{'type':'api','platform':'iphone'},'comments':{'count':0,'can_post':1,'groups_can_post':true},'likes':{'count':33,'user_likes':0,'can_like':1,'can_publish':1},'reposts':{'count':24,'user_reposted':0},'views':{'count':4286},'is_favorite':false,'donut':{'is_donut':false},'short_text_rate':0.800000,'carousel_offset':0,'edited':1618252199}]}}"
ReplaceString(json, "'", Chr(34), #PB_String_InPlace) 

jn = ParseJSON(#PB_Any, json)

Debug "count: "     + JSON::GET(jn, "response\count") 
Debug "item 0 id: " + JSON::GET(jn, "response\items\[0]\id") 

SUPER.
The perfect universal way.
I was hoping there would be a way to work with JSON like this.
Many thanks!


As always helped, thank you all !!!!

Best wishes
Post Reply