SwiftyJSON parsing data in array of objects - json

I am trying to get the value for "name" in the tracks section of JSON. The following JSON is from Spotify. I am using SwiftyJSON for parsing the data.
I am able to pull values from the base json. But once I try to pull anything in the "track" section I get errors.
Here is a sample of the code I got working
let swiftJSON = try? JSON(data: data)
let addedAT = swiftJSON!["items"].arrayValue.map {$0["added_at"].stringValue}
print(addedAT)
{
"items" : [
{
"video_thumbnail" : {
"url" : null
},
"track" : {
"is_playable" : true,
"type" : "track",
"id" : "3F5CgOj3wFlRv51JsHbxhe",
"track" : true,
"duration_ms" : 218364,
"explicit" : true,
"uri" : "spotify:track:3F5CgOj3wFlRv51JsHbxhe",
"is_local" : false,
"artists" : [
{
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/artist\/3TVXtAsR1Inumwj472S9r4"
},
"uri" : "spotify:artist:3TVXtAsR1Inumwj472S9r4",
"type" : "artist",
"href" : "https:\/\/api.spotify.com\/v1\/artists\/3TVXtAsR1Inumwj472S9r4",
"name" : "Drake",
"id" : "3TVXtAsR1Inumwj472S9r4"
},
{
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/artist\/1URnnhqYAYcrqrcwql10ft"
},
"uri" : "spotify:artist:1URnnhqYAYcrqrcwql10ft",
"type" : "artist",
"href" : "https:\/\/api.spotify.com\/v1\/artists\/1URnnhqYAYcrqrcwql10ft",
"name" : "21 Savage",
"id" : "1URnnhqYAYcrqrcwql10ft"
}
],
"album" : {
"name" : "Honestly, Nevermind",
"total_tracks" : 14,
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/album\/3cf4iSSKd8ffTncbtKljXw"
},
"images" : [
{
"height" : 640,
"url" : "https:\/\/i.scdn.co\/image\/ab67616d0000b2738dc0d801766a5aa6a33cbe37",
"width" : 640
},
{
"height" : 300,
"url" : "https:\/\/i.scdn.co\/image\/ab67616d00001e028dc0d801766a5aa6a33cbe37",
"width" : 300
},
{
"height" : 64,
"url" : "https:\/\/i.scdn.co\/image\/ab67616d000048518dc0d801766a5aa6a33cbe37",
"width" : 64
}
],
"artists" : [
{
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/artist\/3TVXtAsR1Inumwj472S9r4"
},
"uri" : "spotify:artist:3TVXtAsR1Inumwj472S9r4",
"type" : "artist",
"href" : "https:\/\/api.spotify.com\/v1\/artists\/3TVXtAsR1Inumwj472S9r4",
"name" : "Drake",
"id" : "3TVXtAsR1Inumwj472S9r4"
}
],
"href" : "https:\/\/api.spotify.com\/v1\/albums\/3cf4iSSKd8ffTncbtKljXw",
"release_date_precision" : "day",
"type" : "album",
"id" : "3cf4iSSKd8ffTncbtKljXw",
"album_type" : "album",
"release_date" : "2022-06-17",
"uri" : "spotify:album:3cf4iSSKd8ffTncbtKljXw"
},
"track_number" : 14,
"external_ids" : {
"isrc" : "USUG12204897"
},
"episode" : false,
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/track\/3F5CgOj3wFlRv51JsHbxhe"
},
"popularity" : 90,
"disc_number" : 1,
"name" : "Jimmy Cooks (feat. 21 Savage)",
"preview_url" : "https:\/\/p.scdn.co\/mp3-preview\/5feee7c2fed489c933a31ef125aa4ffb5205a457?cid=263c670f8c17484d89d8bd2e6e0964f7",
"href" : "https:\/\/api.spotify.com\/v1\/tracks\/3F5CgOj3wFlRv51JsHbxhe"
},
"added_by" : {
"uri" : "spotify:user:12493212",
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/user\/12493212"
},
"type" : "user",
"href" : "https:\/\/api.spotify.com\/v1\/users\/12493212",
"id" : "12493212"
},
"primary_color" : null,
"added_at" : "2022-06-28T16:06:35Z",
"is_local" : false
},
{
"video_thumbnail" : {
"url" : null
},
"track" : {
"is_playable" : true,
"type" : "track",
"id" : "1qMMYpVatbRITKCfq1gasi",
"track" : true,
"duration_ms" : 172933,
"explicit" : true,
"uri" : "spotify:track:1qMMYpVatbRITKCfq1gasi",
"is_local" : false,
"artists" : [
{
"href" : "https:\/\/api.spotify.com\/v1\/artists\/1RyvyyTE3xzB2ZywiAwp0i",
"uri" : "spotify:artist:1RyvyyTE3xzB2ZywiAwp0i",
"name" : "Future",
"id" : "1RyvyyTE3xzB2ZywiAwp0i",
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/artist\/1RyvyyTE3xzB2ZywiAwp0i"
},
"type" : "artist"
}
],
"album" : {
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/album\/6tE9Dnp2zInFij4jKssysL"
},
"href" : "https:\/\/api.spotify.com\/v1\/albums\/6tE9Dnp2zInFij4jKssysL",
"release_date" : "2022-04-29",
"release_date_precision" : "day",
"artists" : [
{
"href" : "https:\/\/api.spotify.com\/v1\/artists\/1RyvyyTE3xzB2ZywiAwp0i",
"uri" : "spotify:artist:1RyvyyTE3xzB2ZywiAwp0i",
"name" : "Future",
"id" : "1RyvyyTE3xzB2ZywiAwp0i",
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/artist\/1RyvyyTE3xzB2ZywiAwp0i"
},
"type" : "artist"
}
],
"name" : "I NEVER LIKED YOU",
"type" : "album",
"album_type" : "album",
"id" : "6tE9Dnp2zInFij4jKssysL",
"uri" : "spotify:album:6tE9Dnp2zInFij4jKssysL",
"images" : [
{
"height" : 640,
"width" : 640,
"url" : "https:\/\/i.scdn.co\/image\/ab67616d0000b27386badd635b69aea887862214"
},
{
"height" : 300,
"width" : 300,
"url" : "https:\/\/i.scdn.co\/image\/ab67616d00001e0286badd635b69aea887862214"
},
{
"height" : 64,
"width" : 64,
"url" : "https:\/\/i.scdn.co\/image\/ab67616d0000485186badd635b69aea887862214"
}
],
"total_tracks" : 16
},
"track_number" : 5,
"external_ids" : {
"isrc" : "USSM12203787"
},
"episode" : false,
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/track\/1qMMYpVatbRITKCfq1gasi"
},
"popularity" : 83,
"disc_number" : 1,
"name" : "PUFFIN ON ZOOTIEZ",
"preview_url" : "https:\/\/p.scdn.co\/mp3-preview\/ff3bd626fd7f0bd6af4a429b3119b06ff0aee089?cid=263c670f8c17484d89d8bd2e6e0964f7",
"href" : "https:\/\/api.spotify.com\/v1\/tracks\/1qMMYpVatbRITKCfq1gasi"
},
"added_by" : {
"external_urls" : {
"spotify" : "https:\/\/open.spotify.com\/user\/12493212"
},
"uri" : "spotify:user:12493212",
"type" : "user",
"id" : "12493212",
"href" : "https:\/\/api.spotify.com\/v1\/users\/12493212"
},
"primary_color" : null,
"added_at" : "2022-06-28T16:53:13Z",
"is_local" : false
}
],
"href" : "https:\/\/api.spotify.com\/v1\/playlists\/1nJflwAwbyhpQzKksqwC7H\/tracks?offset=0&limit=100&market=ES&locale=en-US;q=1.0",
"offset" : 0,
"previous" : null,
"limit" : 100,
"next" : null,
"total" : 2
}

Related

One jq command to get expected output

I want to format below json code and print only specific values.
I tried using below command and got the below json output. But when I further try to parse the code for end result it didn't worked as expected.
So, a little help can make things work for me.
Command that results in below JSON code output:
mgmt_cli -r true -d 192.168.86.201 show access-rulebase name Network --format json | jq --raw-output ' .rulebase[].rulebase[] | {name: .name, rule_number: ."rule-number"}'
JSON Code:
{
"name": "MgmtRule",
"rule_number": 1
}
{
"name": null,
"rule_number": 2
}
{
"name": null,
"rule_number": 3
}
{
"name": "Inside Rules",
"rule_number": 4
}
{
"name": null,
"rule_number": 5
}
{
"name": null,
"rule_number": 6
}
{
"name": null,
"rule_number": 7
}
{
"name": null,
"rule_number": 8
}
{
"name": null,
"rule_number": 9
}
{
"name": "Cleanup rule",
"rule_number": 10
}
Further formatting jq command that I added to the above command to get the end result:
jq --slurp '"\(.[0])-\(.[length -1])"'
Unexpected Output:
"{\"name\":\"MgmtRule\",\"rule_number\":1}-{\"name\":\"Cleanup rule\",\"rule_number\":10}"
Expected Output:
MgmtRule (1-3)
Inside Rules (4-9)
Cleanup rule (10)
Raw output:
Short form of original raw output:
{
"uid" : "38271c2f-ab44-4e25-9aa4-e219cb6e12cf",
"name" : "Network",
"rulebase" : [ {
"uid" : "38acf747-85ee-4962-a3e7-d3cd678c388f",
"name" : "Internal Rules",
"type" : "access-section",
"from" : 1,
"to" : 3,
"rulebase" : [ {
"uid" : "bdd9d868-51b2-4210-8c37-f0237cd560fa",
"name" : "Test",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 1,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
"source-negate" : false,
"destination" : [ "375f4a15-1a7d-0b48-8ffc-2e9eb4e512ae" ],
"destination-negate" : false,
"service" : [ "97aeb443-9aea-11d5-bd16-0090272ccb30", "18ec9eaa-1657-4240-ab97-5f234623336b" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c472",
"action-settings" : {
"enable-identity-captive-portal" : false
},
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674486777583,
"iso-8601" : "2023-01-23T20:42+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674486722309,
"iso-8601" : "2023-01-23T20:42+0530"
},
"creator" : "admin"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
}, {
"uid" : "bb93b38a-c672-49ec-afb0-8a90e8518437",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 2,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
"source-negate" : false,
"destination" : [ "8dbb91f3-786b-4d11-a029-45ee5b7d59cf" ],
"destination-negate" : false,
"service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c472",
"action-settings" : {
"enable-identity-captive-portal" : false
},
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674488952100,
"iso-8601" : "2023-01-23T21:19+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674488917458,
"iso-8601" : "2023-01-23T21:18+0530"
},
"creator" : "admin"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
}, {
"uid" : "925f210e-ce4f-4139-870e-3cd53f46d832",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 3,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9" ],
"source-negate" : false,
"destination" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"destination-negate" : false,
"service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c473",
"action-settings" : { },
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674490197006,
"iso-8601" : "2023-01-23T21:39+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674490169763,
"iso-8601" : "2023-01-23T21:39+0530"
},
"creator" : "admin"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
} ]
}, {
"uid" : "da0fdbf6-360b-44a4-94a8-b01894d3b5a0",
"name" : "cleanup rule",
"type" : "access-section",
"from" : 4,
"to" : 4,
"rulebase" : [ {
"uid" : "2b922948-da96-4c9d-a654-063e0183f9ae",
"name" : "Cleanup rule",
"type" : "access-rule",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"rule-number" : 4,
"track" : {
"type" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"per-session" : false,
"per-connection" : true,
"accounting" : false,
"enable-firewall-session" : false,
"alert" : "none"
},
"source" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"source-negate" : false,
"destination" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"destination-negate" : false,
"service" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"service-negate" : false,
"service-resource" : "",
"vpn" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"action" : "6c488338-8eec-4103-ad21-cd461ac2c473",
"action-settings" : { },
"content" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"content-negate" : false,
"content-direction" : "any",
"time" : [ "97aeb369-9aea-11d5-bd16-0090272ccb30" ],
"custom-fields" : {
"field-1" : "",
"field-2" : "",
"field-3" : ""
},
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674486751969,
"iso-8601" : "2023-01-23T20:42+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1668629634218,
"iso-8601" : "2022-11-17T01:43+0530"
},
"creator" : "System"
},
"comments" : "",
"enabled" : true,
"install-on" : [ "6c488338-8eec-4103-ad21-cd461ac2c476" ],
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "not_supported"
}
} ]
} ],
"objects-dictionary" : [ {
"uid" : "6c488338-8eec-4103-ad21-cd461ac2c472",
"name" : "Accept",
"type" : "RulebaseAction",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "Actions/actionsAccept",
"color" : "none"
}, {
"uid" : "8dbb91f3-786b-4d11-a029-45ee5b7d59cf",
"name" : "All_Internet",
"type" : "address-range",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"ipv4-address-first" : "0.0.0.0",
"ipv4-address-last" : "255.255.255.255",
"icon" : "Objects/ip",
"color" : "sienna"
}, {
"uid" : "97aeb369-9aea-11d5-bd16-0090272ccb30",
"name" : "Any",
"type" : "CpmiAnyObject",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "General/globalsAny",
"color" : "black"
}, {
"uid" : "6c488338-8eec-4103-ad21-cd461ac2c473",
"name" : "Drop",
"type" : "RulebaseAction",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "Actions/actionsDrop",
"color" : "none"
}, {
"uid" : "375f4a15-1a7d-0b48-8ffc-2e9eb4e512ae",
"name" : "dummy",
"type" : "simple-gateway",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"icon" : "NetworkObjects/management",
"color" : "black"
}, {
"uid" : "6ddb84da-ef72-45c8-b2c4-bb3a849a6aa9",
"name" : "H_192.168.86.111",
"type" : "host",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"ipv4-address" : "192.168.86.111",
"icon" : "Objects/host",
"color" : "black"
}, {
"uid" : "97aeb443-9aea-11d5-bd16-0090272ccb30",
"name" : "https",
"type" : "service-tcp",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"port" : "443",
"icon" : "Protocols/HTTP",
"color" : "red"
}, {
"uid" : "598ead32-aa42-4615-90ed-f51a5928d41d",
"name" : "Log",
"type" : "Track",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "Track/tracksLog",
"color" : "none"
}, {
"uid" : "6c488338-8eec-4103-ad21-cd461ac2c476",
"name" : "Policy Targets",
"type" : "Global",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"icon" : "General/globalsAny",
"color" : "none"
}, {
"uid" : "18ec9eaa-1657-4240-ab97-5f234623336b",
"name" : "ssh",
"type" : "service-tcp",
"domain" : {
"uid" : "a0bbbc99-adef-4ef8-bb6d-defdefdefdef",
"name" : "Check Point Data",
"domain-type" : "data domain"
},
"port" : "22",
"icon" : "Services/TCPService",
"color" : "red"
} ],
"from" : 1,
"to" : 4,
"total" : 4
}
Elements that I'm trying to fetch and get the end result from above raw output:
"name" & "rule-number"
Expected Output from above raw data:
Internal Rules (1-3)
cleanup rule (4)
By transforming the unprocessed, raw output generated by mgmt_cli, the solution becomes almost trivial since the JSON already contains the "to" and "from" properties.
Input (shortened to the relevant bits):
{
"uid": "38271c2f-ab44-4e25-9aa4-e219cb6e12cf",
"name": "Network",
"rulebase": [
{
"uid": "38acf747-85ee-4962-a3e7-d3cd678c388f",
"name": "Internal Rules",
"type": "access-section",
"from": 1,
"to": 3
},
{
"uid": "da0fdbf6-360b-44a4-94a8-b01894d3b5a0",
"name": "cleanup rule",
"type": "access-section",
"from": 4,
"to": 4
}
],
"from": 1,
"to": 4,
"total": 4
}
Jq transformation:
... | jq -r '.rulebase[]
| if .from == .to then "\(.name) (\(.from))"
else "\(.name) (\(.from)-\(.to))"
end'
Output:
Internal Rules (1-3)
cleanup rule (4)
Or perhaps (but I find the if/then more straightforward):
.rulebase[]
| (select(.from == .to) | "\(.name) (\(.from)-\(.to))")
// "\(.name) (\(.from)"

Error: JSONDecodeError: Expecting property name enclosed in double quotes

I'm trying to convert a JSON to CSV but now it is giving an error:
'JSONDecodeError: Expecting property name enclosed in double quotes'
The error starts on the line:
jsonDict = json.loads(jsonObj)
And I gave print (jsonObj).
Could someone help me I don't know what else to do.
Heading:
# versao python #3.7.3
import json
from pandas.io.json import json_normalize
def flatten_dict(d, acc={}):
for k, v in d.items():
if isinstance(v, dict):
flatten_dict(v, acc)
elif isinstance(v, list):
for l in v:
flatten_dict(l, acc)
else:
acc[k] = v
return acc
data = []
with open('testejson.json') as f:
for jsonObj in f:
jsonDict = json.loads(jsonObj)
data.append(jsonDict)
df = json_normalize(data)
df.to_csv('tmp.csv', index=False)
The JSON file and very big plus this and its structure:
{
"CVE_data_type" : "CVE",
"CVE_data_format" : "MITRE",
"CVE_data_version" : "4.0",
"CVE_data_numberOfCVEs" : "1052",
"CVE_data_timestamp" : "2021-03-16T16:00Z",
"CVE_Items" : [ {
"cve" : {
"data_type" : "CVE",
"data_format" : "MITRE",
"data_version" : "4.0",
"CVE_data_meta" : {
"ID" : "CVE-2002-0184",
"ASSIGNER" : "cve#mitre.org"
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ {
"lang" : "en",
"value" : "NVD-CWE-Other"
} ]
} ]
},
"references" : {
"reference_data" : [ {
"url" : "http://distro.conectiva.com.br/atualizacoes/?id=a&anuncio=000475",
"name" : "CLA-2002:475",
"refsource" : "CONECTIVA",
"tags" : [ ]
}, {
"url" : "http://marc.info/?l=bugtraq&m=101974610509912&w=2",
"name" : "20020425 [Global InterSec 2002041701] Sudo Password Prompt",
"refsource" : "BUGTRAQ",
"tags" : [ ]
}, {
"url" : "http://marc.info/?l=bugtraq&m=101975443619600&w=2",
"name" : "20020425 Sudo version 1.6.6 now available (fwd)",
"refsource" : "BUGTRAQ",
"tags" : [ ]
}, {
"url" : "http://marc.info/?l=bugtraq&m=101979472822196&w=2",
"name" : "20020425 [slackware-security] sudo upgrade fixes a potential vulnerability",
"refsource" : "BUGTRAQ",
"tags" : [ ]
}, {
"url" : "http://marc.info/?l=bugtraq&m=102010164413135&w=2",
"name" : "20020429 TSLSA-2002-0046 - sudo",
"refsource" : "BUGTRAQ",
"tags" : [ ]
}, {
"url" : "http://www.debian.org/security/2002/dsa-128",
"name" : "DSA-128",
"refsource" : "DEBIAN",
"tags" : [ ]
}, {
"url" : "http://www.iss.net/security_center/static/8936.php",
"name" : "sudo-password-expansion-overflow(8936)",
"refsource" : "XF",
"tags" : [ ]
}, {
"url" : "http://www.kb.cert.org/vuls/id/820083",
"name" : "VU#820083",
"refsource" : "CERT-VN",
"tags" : [ "US Government Resource" ]
}, {
"url" : "http://www.linux-mandrake.com/en/security/2002/MDKSA-2002-028.php3",
"name" : "MDKSA-2002:028",
"refsource" : "MANDRAKE",
"tags" : [ ]
}, {
"url" : "http://www.linuxsecurity.com/advisories/other_advisory-2040.html",
"name" : "ESA-20020429-010",
"refsource" : "ENGARDE",
"tags" : [ "Patch", "Vendor Advisory" ]
}, {
"url" : "http://www.novell.com/linux/security/advisories/2002_014_sudo_txt.html",
"name" : "SuSE-SA:2002:014",
"refsource" : "SUSE",
"tags" : [ ]
}, {
"url" : "http://www.redhat.com/support/errata/RHSA-2002-071.html",
"name" : "RHSA-2002:071",
"refsource" : "REDHAT",
"tags" : [ ]
}, {
"url" : "http://www.redhat.com/support/errata/RHSA-2002-072.html",
"name" : "RHSA-2002:072",
"refsource" : "REDHAT",
"tags" : [ ]
}, {
"url" : "http://www.securityfocus.com/bid/4593",
"name" : "4593",
"refsource" : "BID",
"tags" : [ ]
} ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "Sudo before 1.6.6 contains an off-by-one error that can result in a heap-based buffer overflow that may allow local users to gain root privileges via special characters in the -p (prompt) argument, which are not properly expanded."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ {
"operator" : "OR",
"cpe_match" : [ {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.5.9:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p3:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p4:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p5:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p6:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.3p7:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.4:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.4p1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.4p2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.5:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.5p1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:todd_miller:sudo:1.6.5p2:*:*:*:*:*:*:*"
} ]
} ]
},
"impact" : {
"baseMetricV2" : {
"cvssV2" : {
"version" : "2.0",
"vectorString" : "AV:L/AC:L/Au:N/C:C/I:C/A:C",
"accessVector" : "LOCAL",
"accessComplexity" : "LOW",
"authentication" : "NONE",
"confidentialityImpact" : "COMPLETE",
"integrityImpact" : "COMPLETE",
"availabilityImpact" : "COMPLETE",
"baseScore" : 7.2
},
"severity" : "HIGH",
"exploitabilityScore" : 3.9,
"impactScore" : 10.0,
"obtainAllPrivilege" : true,
"obtainUserPrivilege" : false,
"obtainOtherPrivilege" : false,
"userInteractionRequired" : false
}
},
"publishedDate" : "2002-05-16T04:00Z",
"lastModifiedDate" : "2021-03-16T14:15Z"
} ]
}

Unmarshalling JSON with variable-length data and slices of other JSON objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a question on how Go handles unmarhsalling complex/nested JSON as it appears I have to create the entire data structure in structs prior to unmarshalling.
I am working with a JSON file that follows this format (it is the NVD database, reference here if it is helpful https://nvd.nist.gov/vuln/data-feeds):
"CVE_data_type" : "CVE",
"CVE_data_format" : "MITRE",
"CVE_data_version" : "4.0",
"CVE_data_numberOfCVEs" : "1085",
"CVE_data_timestamp" : "2021-02-24T17:00Z",
"CVE_Items" : [ {
"cve" : {
"data_type" : "CVE",
"data_format" : "MITRE",
"data_version" : "4.0",
"CVE_data_meta" : {
"ID" : "CVE-2011-0762",
"ASSIGNER" : "cve#mitre.org"
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ {
"lang" : "en",
"value" : "CWE-399"
} ]
} ]
},
"references" : {
"reference_data" : [ {
"url" : "ftp://vsftpd.beasts.org/users/cevans/untar/vsftpd-2.3.4/Changelog",
"name" : "ftp://vsftpd.beasts.org/users/cevans/untar/vsftpd-2.3.4/Changelog",
"refsource" : "CONFIRM",
"tags" : [ ]
}, {
"url" : "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=622741",
"name" : "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=622741",
"refsource" : "CONFIRM",
"tags" : [ ]
}, {
"url" : "http://cxib.net/stuff/vspoc232.c",
"name" : "http://cxib.net/stuff/vspoc232.c",
"refsource" : "MISC",
"tags" : [ "Exploit" ]
}, {
"url" : "http://jvn.jp/en/jp/JVN37417423/index.html",
"name" : "JVN#37417423",
"refsource" : "JVN",
"tags" : [ ]
}, {
"url" : "http://lists.fedoraproject.org/pipermail/package-announce/2011-March/055881.html",
"name" : "FEDORA-2011-2615",
"refsource" : "FEDORA",
"tags" : [ ]
}, {
"url" : "http://lists.fedoraproject.org/pipermail/package-announce/2011-March/055882.html",
"name" : "FEDORA-2011-2590",
"refsource" : "FEDORA",
"tags" : [ ]
}, {
"url" : "http://lists.fedoraproject.org/pipermail/package-announce/2011-March/055957.html",
"name" : "FEDORA-2011-2567",
"refsource" : "FEDORA",
"tags" : [ ]
}, {
"url" : "http://lists.opensuse.org/opensuse-security-announce/2011-05/msg00005.html",
"name" : "SUSE-SR:2011:009",
"refsource" : "SUSE",
"tags" : [ ]
}, {
"url" : "http://marc.info/?l=bugtraq&m=133226187115472&w=2",
"name" : "HPSBMU02752",
"refsource" : "HP",
"tags" : [ ]
}, {
"url" : "http://securityreason.com/achievement_securityalert/95",
"name" : "20110301 vsftpd 2.3.2 remote denial-of-service",
"refsource" : "SREASONRES",
"tags" : [ "Exploit" ]
}, {
"url" : "http://securityreason.com/securityalert/8109",
"name" : "8109",
"refsource" : "SREASON",
"tags" : [ ]
}, {
"url" : "http://www.debian.org/security/2011/dsa-2305",
"name" : "DSA-2305",
"refsource" : "DEBIAN",
"tags" : [ ]
}, {
"url" : "http://www.exploit-db.com/exploits/16270",
"name" : "16270",
"refsource" : "EXPLOIT-DB",
"tags" : [ ]
}, {
"url" : "http://www.kb.cert.org/vuls/id/590604",
"name" : "VU#590604",
"refsource" : "CERT-VN",
"tags" : [ "US Government Resource" ]
}, {
"url" : "http://www.mandriva.com/security/advisories?name=MDVSA-2011:049",
"name" : "MDVSA-2011:049",
"refsource" : "MANDRIVA",
"tags" : [ ]
}, {
"url" : "http://www.redhat.com/support/errata/RHSA-2011-0337.html",
"name" : "RHSA-2011:0337",
"refsource" : "REDHAT",
"tags" : [ ]
}, {
"url" : "http://www.securityfocus.com/archive/1/516748/100/0/threaded",
"name" : "20110301 vsftpd 2.3.2 remote denial-of-service",
"refsource" : "BUGTRAQ",
"tags" : [ ]
}, {
"url" : "http://www.securityfocus.com/bid/46617",
"name" : "46617",
"refsource" : "BID",
"tags" : [ "Exploit" ]
}, {
"url" : "http://www.securitytracker.com/id?1025186",
"name" : "1025186",
"refsource" : "SECTRACK",
"tags" : [ ]
}, {
"url" : "http://www.ubuntu.com/usn/USN-1098-1",
"name" : "USN-1098-1",
"refsource" : "UBUNTU",
"tags" : [ ]
}, {
"url" : "http://www.vupen.com/english/advisories/2011/0547",
"name" : "ADV-2011-0547",
"refsource" : "VUPEN",
"tags" : [ ]
}, {
"url" : "http://www.vupen.com/english/advisories/2011/0639",
"name" : "ADV-2011-0639",
"refsource" : "VUPEN",
"tags" : [ ]
}, {
"url" : "http://www.vupen.com/english/advisories/2011/0668",
"name" : "ADV-2011-0668",
"refsource" : "VUPEN",
"tags" : [ ]
}, {
"url" : "http://www.vupen.com/english/advisories/2011/0713",
"name" : "ADV-2011-0713",
"refsource" : "VUPEN",
"tags" : [ ]
}, {
"url" : "https://exchange.xforce.ibmcloud.com/vulnerabilities/65873",
"name" : "vsftpd-vsffilenamepassesfilter-dos(65873)",
"refsource" : "XF",
"tags" : [ ]
} ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "The vsf_filename_passes_filter function in ls.c in vsftpd before 2.3.3 allows remote authenticated users to cause a denial of service (CPU consumption and process slot exhaustion) via crafted glob expressions in STAT commands in multiple FTP sessions, a different vulnerability than CVE-2010-2632."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ {
"operator" : "OR",
"cpe_match" : [ {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.3:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.4:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.5:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.6:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.7:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.8:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.9:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.10:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.11:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.12:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.13:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.14:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.0.15:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.9.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.9.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.9.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:0.9.3:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.0.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.0.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.1.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.1.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.1.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.1.3:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.2.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.2.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:1.2.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.3:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.4:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.5:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.6:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.0.7:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.1.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.1.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.1.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.2.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.2.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.2.2:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.3.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:2.3.1:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:beasts:vsftpd:*:*:*:*:*:*:*:*",
"versionEndIncluding" : "2.3.2"
} ]
} ]
},
"impact" : {
"baseMetricV2" : {
"cvssV2" : {
"version" : "2.0",
"vectorString" : "AV:N/AC:L/Au:S/C:N/I:N/A:P",
"accessVector" : "NETWORK",
"accessComplexity" : "LOW",
"authentication" : "SINGLE",
"confidentialityImpact" : "NONE",
"integrityImpact" : "NONE",
"availabilityImpact" : "PARTIAL",
"baseScore" : 4.0
},
"severity" : "MEDIUM",
"exploitabilityScore" : 8.0,
"impactScore" : 2.9,
"obtainAllPrivilege" : false,
"obtainUserPrivilege" : false,
"obtainOtherPrivilege" : false,
"userInteractionRequired" : false
}
},
"publishedDate" : "2011-03-02T20:00Z",
"lastModifiedDate" : "2021-02-19T05:15Z"
}, {
"cve" : {
"data_type" : "CVE",
"data_format" : "MITRE",
"data_version" : "4.0",
"CVE_data_meta" : {
"ID" : "CVE-2011-4362",
"ASSIGNER" : "cve#mitre.org"
},
"problemtype" : {
"problemtype_data" : [ {
"description" : [ {
"lang" : "en",
"value" : "CWE-189"
} ]
} ]
},
"references" : {
"reference_data" : [ {
"url" : "http://archives.neohapsis.com/archives/bugtraq/2011-12/0167.html",
"name" : "20111224 Lighttpd Proof of Concept code for CVE-2011-4362",
"refsource" : "BUGTRAQ",
"tags" : [ "Broken Link" ]
}, {
"url" : "http://blog.pi3.com.pl/?p=277",
"name" : "http://blog.pi3.com.pl/?p=277",
"refsource" : "MISC",
"tags" : [ "Third Party Advisory" ]
}, {
"url" : "http://download.lighttpd.net/lighttpd/security/lighttpd_sa_2011_01.txt",
"name" : "http://download.lighttpd.net/lighttpd/security/lighttpd_sa_2011_01.txt",
"refsource" : "CONFIRM",
"tags" : [ "Vendor Advisory" ]
}, {
"url" : "http://jvn.jp/en/jp/JVN37417423/index.html",
"name" : "JVN#37417423",
"refsource" : "JVN",
"tags" : [ ]
}, {
"url" : "http://redmine.lighttpd.net/issues/2370",
"name" : "http://redmine.lighttpd.net/issues/2370",
"refsource" : "CONFIRM",
"tags" : [ "Vendor Advisory" ]
}, {
"url" : "http://secunia.com/advisories/47260",
"name" : "47260",
"refsource" : "SECUNIA",
"tags" : [ "Third Party Advisory" ]
}, {
"url" : "http://www.debian.org/security/2011/dsa-2368",
"name" : "DSA-2368",
"refsource" : "DEBIAN",
"tags" : [ "Third Party Advisory" ]
}, {
"url" : "http://www.exploit-db.com/exploits/18295",
"name" : "18295",
"refsource" : "EXPLOIT-DB",
"tags" : [ "Third Party Advisory", "VDB Entry" ]
}, {
"url" : "http://www.openwall.com/lists/oss-security/2011/11/29/13",
"name" : "[oss-security] 20111129 Re: CVE Request: lighttpd/mod_auth out-of-bounds read due to signedness error",
"refsource" : "MLIST",
"tags" : [ "Mailing List", "Third Party Advisory" ]
}, {
"url" : "http://www.openwall.com/lists/oss-security/2011/11/29/8",
"name" : "[oss-security] 20111129 CVE Request: lighttpd/mod_auth out-of-bounds read due to signedness error",
"refsource" : "MLIST",
"tags" : [ "Mailing List", "Third Party Advisory" ]
}, {
"url" : "http://www.securitytracker.com/id?1026359",
"name" : "1026359",
"refsource" : "SECTRACK",
"tags" : [ "Third Party Advisory", "VDB Entry" ]
}, {
"url" : "https://bugzilla.redhat.com/show_bug.cgi?id=758624",
"name" : "https://bugzilla.redhat.com/show_bug.cgi?id=758624",
"refsource" : "CONFIRM",
"tags" : [ "Issue Tracking", "Third Party Advisory" ]
}, {
"url" : "https://exchange.xforce.ibmcloud.com/vulnerabilities/71536",
"name" : "lighttpd-base64-dos(71536)",
"refsource" : "XF",
"tags" : [ "Third Party Advisory", "VDB Entry" ]
} ]
},
"description" : {
"description_data" : [ {
"lang" : "en",
"value" : "Integer signedness error in the base64_decode function in the HTTP authentication functionality (http_auth.c) in lighttpd 1.4 before 1.4.30 and 1.5 before SVN revision 2806 allows remote attackers to cause a denial of service (segmentation fault) via crafted base64 input that triggers an out-of-bounds read with a negative index."
} ]
}
},
"configurations" : {
"CVE_data_version" : "4.0",
"nodes" : [ {
"operator" : "OR",
"cpe_match" : [ {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:lighttpd:lighttpd:*:*:*:*:*:*:*:*",
"versionStartIncluding" : "1.4.1",
"versionEndExcluding" : "1.4.30"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:a:lighttpd:lighttpd:1.5.0:*:*:*:*:*:*:*"
} ]
}, {
"operator" : "OR",
"cpe_match" : [ {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:o:debian:debian_linux:5.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:o:debian:debian_linux:6.0:*:*:*:*:*:*:*"
}, {
"vulnerable" : true,
"cpe23Uri" : "cpe:2.3:o:debian:debian_linux:7.0:*:*:*:*:*:*:*"
} ]
} ]
},
"impact" : {
"baseMetricV2" : {
"cvssV2" : {
"version" : "2.0",
"vectorString" : "AV:N/AC:L/Au:N/C:N/I:N/A:P",
"accessVector" : "NETWORK",
"accessComplexity" : "LOW",
"authentication" : "NONE",
"confidentialityImpact" : "NONE",
"integrityImpact" : "NONE",
"availabilityImpact" : "PARTIAL",
"baseScore" : 5.0
},
"severity" : "MEDIUM",
"exploitabilityScore" : 10.0,
"impactScore" : 2.9,
"obtainAllPrivilege" : false,
"obtainUserPrivilege" : false,
"obtainOtherPrivilege" : false,
"userInteractionRequired" : false
}
},
"publishedDate" : "2011-12-24T19:55Z",
"lastModifiedDate" : "2021-02-19T05:15Z"
},
As you can see, there is a lot of data here, and I am struggling to get this unmarshalled into something that I can work with. The first few fields are straightforward, so I have begun creating a struct as such:
type NvdData struct {
CveDataType string `json:"CVE_data_type"`
CveDataFormat string `json:"CVE_data_format"`
CveDataVersion string `json:"CVE_data_version"`
CveDataNumberCves string `json:"CVE_data_numberOfCVEs"`
}
Where I am confused is when we get to the "CVE_Items" field which contains a list/slice of "CVE" objects that then all have their own various fields, some of which are lists, some which aren't.
My question is, how do I construct my NvdData struct to handle this? Do I need to create an additional struct, CVE , which has all of the various fields associated with each CVE entry, and include that data structure as part of my NvdData definition? Something like:
type NvdData struct {
CveDataType string `json:"CVE_data_type"`
CveDataFormat string `json:"CVE_data_format"`
CveDataVersion string `json:"CVE_data_version"`
CveDataNumberCves string `json:"CVE_data_numberOfCVEs"`
CveItems []Cve{} `json:"CVE_Items`
}
type Cve struct {
DataType string `json:"data_type"`
DataFormat string `json:"data_format"`
...
}
If that makes sense? And then for any subsequent data structures that are lists/slices, I would have to describe those as a struct as well and the type for that json field would be a slice of that type?
One last one-off question I have as well is, if there are fields that I don't care about can I drop them from the type definition and they will be ignored by the Unmarshalling process, or do I need to describe them as well and just ignore them when I iterate through the data?
I saw that there is a schema online for this: https://csrc.nist.gov/schema/nvd/feed/1.1/nvd_cve_feed_json_1.1.schema
Which has this, which defines a CVE item (it references other definitions in the same file). You could in theory use this to create the various structs. There is an attempt to auto generate the structs here: https://adrianhesketh.com/2016/07/19/json-schema-to-go-struct-generator-roundup/
"def_cve_item": {
"description": "Defines a vulnerability in the NVD data feed.",
"properties": {
"cve": {"$ref": "CVE_JSON_4.0_min_1.1.schema"},
"configurations": {"$ref": "#/definitions/def_configurations"},
"impact": {"$ref": "#/definitions/def_impact"},
"publishedDate": {"type": "string"},
"lastModifiedDate": {"type": "string"}
},
"required": ["cve"]
}
I think the simplest (albeit not the most robust) would be to unmarshall into a map[string]interface{}
type CVEItem struct {
CVE map[string]interface{} `json:"cve"`
Configurations map[string]interface{} `json:"configurations"`
Impact map[string]interface{} `json:"impact"`
PublishedDate string `json:"publishedDate"`
LastModifiedDate string `json:"lastModifiedDate"`
}
type DataStruct struct {
DataType string `json:"CVE_data_type"`
CVEItems []CVEItem `json:"CVE_items"`
}
var result DataStruct
err = json.Unmarshal([]byte(byteValue), &result)
if err != nil {
fmt.Println(err)
}
Then from this you can loop through the various items and build concrete structs from what is there (by looking at the keys). If there are many optional or nested items... then yes, it will be a pain to do.

I am trying to access room_type_info from json data but unable to do in swift [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
JSON:
"results" : [ {
"property_code" : "WYBOSCHH",
"property_name" : "Wyndham Boston Chelsea",
"location" : {
"latitude" : 42.39795,
"longitude" : -71.03977
},
"address" : {
"line1" : "201 Everett Avenue",
"city" : "Chelsea",
"region" : "MA",
"postal_code" : "02150-1816",
"country" : "US"
},
"total_price" : {
"amount" : "287.30",
"currency" : "USD"
},
"min_daily_rate" : {
"amount" : "71.82",
"currency" : "USD"
},
"contacts" : [ {
"type" : "PHONE",
"detail" : "1-617-8842900"
}, {
"type" : "FAX",
"detail" : "1-617-8847888"
} ],
"amenities" : [ {
"amenity" : "ICE_MACHINE",
"ota_code" : 52,
"description" : "Ice machine"
}, {
"amenity" : "RESTAURANT",
"ota_code" : 76,
"description" : "Restaurant"
}, {
"amenity" : "BABY_SITTING",
"ota_code" : 8,
"description" : "Baby sitting"
}, {
"amenity" : "BEAUTY_SALON",
"ota_code" : 107,
"description" : "Beauty shop/salon"
}, {
"amenity" : "ELEVATORS",
"ota_code" : 33,
"description" : "Elevators"
}, {
"amenity" : "INTERNET_PUBLIC_AREAS",
"ota_code" : 178,
"description" : "Internet access in public areas"
}, {
"amenity" : "JACUZZI",
"ota_code" : 55,
"description" : "Jacuzzi"
}, {
"amenity" : "LAUNDRY_SERVICE",
"ota_code" : 58,
"description" : "Laundry/Valet service"
}, {
"amenity" : "PARKING",
"ota_code" : 68,
"description" : "Parking"
}, {
"amenity" : "LOUNGE_BARS",
"ota_code" : 165,
"description" : "Lounges/bars"
}, {
"amenity" : "POOL",
"ota_code" : 71,
"description" : "Pool"
}, {
"amenity" : "OUTDOOR_POOL",
"ota_code" : 66,
"description" : "Outdoor pool"
} ],
"awards" : [ {
"provider" : "Local Star Rating",
"rating" : "3"
}, {
"provider" : "AAA",
"rating" : "3"
} ],
"images" : [ {
"category" : "MAIN_IMAGE",
"width" : 70,
"height" : 70,
"url" : "http://multimediarepository.amadeus.com/cmr/retrieve/hotel/5811669CC5364943965EC9FB3D56642C/A"
}, {
"category" : "MAIN_IMAGE",
"width" : 480,
"height" : 260,
"url" : "http://multimediarepository.amadeus.com/cmr/retrieve/hotel/5811669CC5364943965EC9FB3D56642C/J"
} ],
"rooms" : [ {
"booking_code" : "N2DA01",
"room_type_code" : "N2D",
"rate_plan_code" : "A01",
"total_amount" : {
"amount" : "287.30",
"currency" : "USD"
},
"rates" : [ {
"start_date" : "2016-03-14",
"end_date" : "2016-03-16",
"currency_code" : "USD",
"price" : 143.65
} ],
"descriptions" : [ "PAY NOW GET 200 BONUS POINTS", "2 DOUBLE BED ROOM, WIFI, 32 INCH FLAT SCREEN", "TV." ],
"room_type_info" : {
"room_type" : "Non-smoking",
"bed_type" : "Double",
"number_of_beds" : "2"
},
"rate_type_code" : "A01"
} ],
"_links" : {
"more_rooms_at_this_hotel" : {
"href" : "http://api.sandbox.amadeus.com/v1.2/hotels/WYBOSCHH?apikey=zO8hvuhC5ejQBwJgMcGzS19fGpExrRuQ&check_in=2016-03-14&check_out=2016-03-16"
}
}
}, {
"property_code" : "HYBOSHHS",
"property_name" : "Hyatt Regency Boston Harbor",
"location" : {
"latitude" : 42.35914,
"longitude" : -71.02742
},
"address" : {
"line1" : "101 Harborside Dr",
"city" : "Boston",
"region" : "MA",
"postal_code" : "02128",
"country" : "US"
},
"total_price" : {
"amount" : "297.44",
"currency" : "USD"
},
"min_daily_rate" : {
"amount" : "139.92",
"currency" : "USD"
},
"contacts" : [ {
"type" : "PHONE",
"detail" : "1-617-568-1234"
}, {
"type" : "FAX",
"detail" : "1-617-567-8856"
} ],
"amenities" : [ {
"amenity" : "COFFEE_SHOP",
"ota_code" : 20,
"description" : "Coffee shop"
}, {
"amenity" : "RESTAURANT",
"ota_code" : 76,
"description" : "Restaurant"
}, {
"amenity" : "ACCESSIBLE_FACILITIES",
"ota_code" : 47,
"description" : "Accessible facilities"
}, {
"amenity" : "BABY_SITTING",
"ota_code" : 8,
"description" : "Baby sitting"
}, {
"amenity" : "CAR_RENTAL",
"ota_code" : 15,
"description" : "Car rental desk"
}, {
"amenity" : "CHILDREN_WELCOME",
"ota_code" : 218,
"description" : "Children welcome"
}, {
"amenity" : "ELEVATORS",
"ota_code" : 33,
"description" : "Elevators"
}, {
"amenity" : "CURRENCY_EXCHANGE",
"ota_code" : 26,
"description" : "Currency exchange"
}, {
"amenity" : "GIFT_SHOP",
"ota_code" : 45,
"description" : "Gift/News stand"
}, {
"amenity" : "FREE_HIGH_SPEED_INTERNET",
"ota_code" : 222,
"description" : "Free high speed internet connection"
}, {
"amenity" : "INTERNET_PUBLIC_AREAS",
"ota_code" : 178,
"description" : "Internet access in public areas"
}, {
"amenity" : "PARKING",
"ota_code" : 68,
"description" : "Parking"
}, {
"amenity" : "PETS_ALLOWED",
"ota_code" : 224,
"description" : "Pets allowed"
}, {
"amenity" : "VALET_PARKING",
"ota_code" : 97,
"description" : "Valet parking "
}, {
"amenity" : "LOUNGE_BARS",
"ota_code" : 165,
"description" : "Lounges/bars"
}, {
"amenity" : "LOUNGE_BARS",
"ota_code" : 165,
"description" : "Lounges/bars"
}, {
"amenity" : "ROOM_SERVICE",
"ota_code" : 77,
"description" : "Room service"
}, {
"amenity" : "SAFE_DEPOSIT_BOX",
"ota_code" : 78,
"description" : "Safe deposit box"
}, {
"amenity" : "BALLROOM",
"ota_code" : 191,
"description" : "Ballroom"
} ],
"awards" : [ ],
"images" : [ {
"category" : "MAIN_IMAGE",
"width" : 70,
"height" : 70,
"url" : "http://multimediarepository.amadeus.com/cmr/retrieve/hotel/AE3C4B469985434AB8E48B23FD4A50CD/A"
}, {
"category" : "MAIN_IMAGE",
"width" : 320,
"height" : 480,
"url" : "http://multimediarepository.amadeus.com/cmr/retrieve/hotel/AE3C4B469985434AB8E48B23FD4A50CD/J"
} ],
"rooms" : [ {
"booking_code" : "KNGADPR",
"room_type_code" : "A1K",
"rate_plan_code" : "***",
"total_amount" : {
"amount" : "297.44",
"currency" : "USD"
},
"rates" : [ {
"start_date" : "2016-03-14",
"end_date" : "2016-03-15",
"currency_code" : "USD",
"price" : 139.92
}, {
"start_date" : "2016-03-15",
"end_date" : "2016-03-16",
"currency_code" : "USD",
"price" : 157.52
} ],
"descriptions" : [ "ADV PURCHASE* AVIATION KING", "1 KING BED: COFFEE MAKER:DATAPORT:" ],
"room_type_info" : {
"room_type" : "Room with bath - Superior",
"bed_type" : "King",
"number_of_beds" : "1"
},
"rate_type_code" : "***"
} ],
"_links" : {
"more_rooms_at_this_hotel" : {
"href" : "http://api.sandbox.amadeus.com/v1.2/hotels/HYBOSHHS?apikey=zO8hvuhC5ejQBwJgMcGzS19fGpExrRuQ&check_in=2016-03-14&check_out=2016-03-16"
}
}
} ]
}
Code:
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "GET"
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: request){(data, response, error) in
if (error != nil) {
print("Error")
}
else {
do {
let fetchedData = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! NSDictionary
let results = fetchedData["results"]! as! NSArray
for eachFetchedHotel in results {
let eachHotel = eachFetchedHotel as! [String: Any]
let hotel_name = eachHotel["property_name"] as! String
for eachRoomInfo in eachHotel["rooms"] {
// if let roomTypeInfo = eachRoomInfo["room_type_info"] as! Int{
// let occupancy_type = roomTypeInfo["number_of_beds"] as! Int
// }
for item in eachRoomInfo["room_type_info"]{
let occupancy_type = item["number_of_beds"]
}
}
// let occupanct_type = eachHotel["rooms"]["room_type_info"]["number_of_beds"] as! Int
let price = eachHotel["total_price.currency" + "total_price.amount"] as! Int
let image = eachHotel["images.url"] as! NSData
self.fetchedHotelData.append(Room(name: hotel_name, occupancy: occupancy_type, price: price, vacant_status: true, image: image))
}
print(self.fetchedHotelData)
}
catch{
print("Error 2")
}
}
}
I recommend you using Alamofire (a powerful networking framework) and SwiftyJSON (for parsing JSON), they are very popular for iOS development.
With SwiftyJSON, you do not need to handle the error or worry about nested JSON, just do something like:
json["data"]["room_type_infor"].stringValue.

How to convert JSON to POJO

Anyway to create a Java POJO template from a JSON string? I'm using Jersey
{
"reviewData" : [ {
"projectKey" : "CR-FOO",
"name" : "Example review.",
"description" : "Description or statement of objectives for this example review.",
"author" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"moderator" : {
"userName" : "scott",
"displayName" : "Scott the Moderator",
"avatarUrl" : "http://foo.com/avatar"
},
"creator" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"permaId" : {
"id" : "CR-FOO-21"
},
"permaIdHistory" : [ "CR-FOO-21" ],
"type" : "REVIEW",
"allowReviewersToJoin" : true,
"metricsVersion" : 4,
"createDate" : "2013-10-08T15:46:11.022+0200",
"dueDate" : "2013-10-09T15:46:11.022+0200",
"jiraIssueKey" : "FOO-6754"
}, {
"projectKey" : "CR-FOO",
"name" : "Example review.",
"description" : "Description or statement of objectives for this example review.",
"author" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"moderator" : {
"userName" : "scott",
"displayName" : "Scott the Moderator",
"avatarUrl" : "http://foo.com/avatar"
},
"creator" : {
"userName" : "joe",
"displayName" : "Joe Krustofski",
"avatarUrl" : "http://foo.com/avatar"
},
"permaId" : {
"id" : "CR-FOO-21"
},
"permaIdHistory" : [ "CR-FOO-21" ],
"type" : "REVIEW",
"allowReviewersToJoin" : true,
"metricsVersion" : 4,
"createDate" : "2013-10-08T15:46:11.022+0200",
"dueDate" : "2013-10-09T15:46:11.022+0200",
"jiraIssueKey" : "FOO-6754"
} ]
}
The best solution I found was http://www.jsonschema2pojo.org/
It has support for Jackson and Maven and an online tool as well.