1

I have data from two APIs in JSON format and parsing them into 2 dictionaries (d1, d2).

I need to derive two additional fields FieldName(from d1), attrVal (from d2) by mapping subAttrDefId present in d2 ("attrValues") with subAttribDefId present in d2.

I have tried below code, but it's giving me "BoundsError"


resp = HTTP.get(URL1)
resp2 = HTTP.get(URL2)
global dict1, dict2

jsonStr = String(resp.body)
dict1 = JSON.parse(jsonStr)

jsonStr2 = String(resp2.body)
dict2 = JSON.parse(jsonStr2)

field_names = values(dict1)

    for item in dict2
        # for field_name in field_names
        #     item[field_name] = missing
        # end
        attr_vals = item["attrValues"]
        for attr_val in attr_vals
            field_name = dict1[attr_val["subAttrDefId"]]
            item[field_name] = attr_val["attr_val"]
            delete!(item,"attrValues")
        end
    end

Sample data from 1st API(d1):

[
   {
      "DefId":4327,
      "DefVersionId":4326,
      "attrValues":[
         {
            "subAttrValId":4328,
            "DefId":4327,
            "subAttrDefId":3888,
            "attrVal":"276",
            "crtUserid":"3456789",
            "crtTmstmp":null,
            "updtUserid":null,
            "updtTmstmp":"2023-01-10T14:08:36.000+00:00"
         },
         {
            "subAttrValId":4329,
            "DefId":4327,
            "subAttrDefId":3889,
            "attrVal":"52",
            "crtUserid":"3456789",
            "crtTmstmp":null,
            "updtUserid":null,
            "updtTmstmp":"2023-01-10T14:08:36.000+00:00"
         },
         {
            "subAttrValId":4330,
            "DefId":4327,
            "subAttrDefId":3890,
            "attrVal":"50",
            "crtUserid":"3456789",
            "crtTmstmp":null,
            "updtUserid":null,
            "updtTmstmp":"2023-01-10T14:08:36.000+00:00"
         },
         {
            "subAttrValId":4331,
            "DefId":4327,
            "subAttrDefId":3891,
            "attrVal":"24",
            "crtUserid":"3456789",
            "crtTmstmp":null,
            "updtUserid":null,
            "updtTmstmp":"2023-01-10T14:08:36.000+00:00"
         }
],
      "code":"AB",
      "typeCode":"00",
      "crtUserid":"12345",
      "crtTmstmp":null,
      "updtUserid":"23456",
      "updtTmstmp":"2023-01-10T14:08:36.000+00:00"
   }
]

Sample data from 2nd API(d2):

[
   {
      "subAttribDefId":3886,
      "displayLabel":"string",
      "attribDesc":"string",
      "attribName":"string",
      "attribDataType":"string",
      "attrFormat":"string",
      "crtUserid":"string",
      "crtTmstmp":"2022-12-21T19:07:14.000+00:00",
      "updtUserid":"string",
      "updtTmstmp":"2022-12-21T19:07:14.000+00:00",
      "FieldName":null,
      "category":null,
      "subcategory":null
   },
   {
      "subAttribDefId":3887,
      "displayLabel":"string",
      "attribDesc":"string",
      "attribName":"string",
      "attribDataType":"string",
      "attrFormat":"string",
      "crtUserid":"string",
      "crtTmstmp":"2022-12-21T19:08:18.000+00:00",
      "updtUserid":"string",
      "updtTmstmp":"2022-12-21T19:08:18.000+00:00",
      "FieldName":null,
      "category":null,
      "subcategory":null
   },
   {
      "subAttribDefId":3888,
      "displayLabel":"Number of bucket",
      "attribDesc":"Number of bucket",
      "attribName":"numbuckets",
      "attribDataType":"number",
      "attrFormat":"",
      "crtUserid":"12345",
      "crtTmstmp":"2022-12-21T19:08:56.000+00:00",
      "updtUserid":null,
      "updtTmstmp":"2022-12-21T19:08:56.000+00:00",
      "FieldName":null,
      "category":null,
      "subcategory":null
   },
   {
      "subAttribDefId":3889,
      "displayLabel":"Cabin",
      "attribDesc":"class Cabin",
      "attribName":"aCabin",
      "attribDataType":"number",
      "attrFormat":"",
      "crtUserid":"1234",
      "crtTmstmp":"2022-12-21T19:08:56.000+00:00",
      "updtUserid":null,
      "updtTmstmp":"2022-12-21T19:08:56.000+00:00",
      "FieldName":null,
      "category":null,
      "subcategory":null
   }

**Dict1**:

Any[Dict{String, Any}("crtTmstmp" => "2023-01-12T08:17:49.000+00:00", "attribName" => "buckets", "subAttribDefId" => 1, "attrFormat" => "^[\\d]*\$", "updtTmstmp" => "2023-01-12T08:17:49.000+00:00", "displayLabel" => "buckets", "crtUserid" => "23456", "FieldName" => nothing, "attribDesc" => "buckets", "category" => "Seat Attributes", 
"updtUserid" => nothing, "attribDataType" => "number", "subcategory" => nothing), Dict{String, Any}("crtTmstmp" => "2023-01-12T08:17:49.000+00:00", "attribName" => "fCabin", "subAttribDefId" => 2, "attrFormat" => "^[\\d]*\$", "updtTmstmp" => "2023-01-12T08:17:49.000+00:00", "displayLabel" => "F Cabin", "crtUserid" => "23456", "FieldName" => nothing, "attribDesc" => "First class Cabin", "category" => "Seat Attributes", "updtUserid" => nothing, "attribDataType" => "number", "subcategory" => nothing), Dict{String, Any}("crtTmstmp" => "2023-01-12T08:17:49.000+00:00", "attribName" => "jCabin", "subAttribDefId" => 3, "attrFormat" => "^[\\d]*\$", "updtTmstmp" => "2023-01-12T08:17:49.000+00:00", "displayLabel" => "J Cabin", "crtUserid" => "23456", "FieldName" => nothing, "attribDesc" => "J Cabin", "category" => "Seat Attributes", "updtUserid" => nothing, "attribDataType" => "number", "subcategory" => nothing), Dict{String, Any}("crtTmstmp" => "2023-01-12T08:17:49.000+00:00", "attribName" => "oCabin", "subAttribDefId" => 4, "attrFormat" => "^[\\d]*\$", "updtTmstmp" => "2023-01-12T08:17:49.000+00:00", "displayLabel" => "O Cabin", "crtUserid" => "23456", "FieldName" => nothing, "attribDesc" => "O Cabin", "category" => "Seat Attributes", "updtUserid" => nothing, "attribDataType" => "number", "subcategory" => nothing), Dict{String, Any}("crtTmstmp" => "2023-01-12T08:17:49.000+00:00", "attribName" => "yPlusCabin", "subAttribDefId" 
=> 5, "attrFormat" => "^[\\d]*\$", "updtTmstmp" => "2023-01-12T08:17:49.000+00:00", "displayLabel" => "Y+ Cabin", "crtUserid" => "23456", "FieldName" => nothing, "attribDesc" => "Y Plus Cabin", "category" => "Seat Attributes", "updtUserid" => nothing, "attribDataType" => "number", "subcategory" => nothing)]

**Dict2:**
Any[Dict{String, Any}("proxyCode" => "000", "crtTmstmp" => nothing, "equipCode" => "ABC", "proxyequipCode" => "ABC", "reportequipcode" => "ABC", "userTypeCode" => "ABC", "code" => "AB", "isUserDefined" => 0, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "ftype" => "000", "crtUserid" => "123456", "attrValues" => Any[Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "276", "subAttribValId" => 4328, "crtUserid" => "123456", "subAttribDefId" => 3888, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "52", "subAttribValId" => 4329, "crtUserid" => "123456", "subAttribDefId" => 3889, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "50", "subAttribValId" => 4330, "crtUserid" => "123456", "subAttribDefId" => 3890, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "24", "subAttribValId" => 4331, "crtUserid" => "123456", "subAttribDefId" => 3891, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "46", "subAttribValId" => 4332, "crtUserid" => "123456", "subAttribDefId" => 3892, "subFletDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "202", "subAttribValId" => 4333, "crtUserid" => "123456", "subAttribDefId" => 3893, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "2", "subAttribValId" => 4334, "crtUserid" => "123456", "subAttribDefId" => 3894, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "Normal", "subAttribValId" => 4335, "crtUserid" => "123456", "subAttribDefId" => 3895, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "8", "subAttribValId" => 4336, "crtUserid" => "123456", "subAttribDefId" => 3896, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "180", "subAttribValId" => 4337, "crtUserid" => "123456", "subAttribDefId" => 3897, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "218", "subAttribValId" => 4338, "crtUserid" => "123456", "subAttribDefId" => 3898, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "9", "subAttribValId" => 4339, "crtUserid" => "123456", "subAttribDefId" => 3899, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "181", "subAttribValId" => 4340, "crtUserid" => "123456", "subAttribDefId" => 3900, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "-", "subAttribValId" => 4341, "crtUserid" => "123456", "subAttribDefId" => 3901, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "40000", "subAttribValId" => 4342, "crtUserid" => "123456", "subAttribDefId" => 3902, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "2.71", "subAttribValId" => 4343, "crtUserid" => "123456", "subAttribDefId" => 3903, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "2.96", "subAttribValId" => 4344, "crtUserid" => "123456", "subAttribDefId" => 3904, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "2%", "subAttribValId" => 4345, "crtUserid" => "123456", "subAttribDefId" => 3905, "subDefId" => 4327), Dict{String, Any}("crtTmstmp" => nothing, "updtUserid" => nothing, "updtTmstmp" => "2023-01-10T14:08:36.000+00:00", "attrVal" => "-3%", "subAttribValId" => 4346, "crtUserid" => "123456", "subAttribDefId" => 3906, "subDefId" => 4327)], "bodyType" => "W", "DefVersionId" => 4326, "subDefId" => 4327, "updtUserid" => "456789")]
3
  • The top level JSON here is a Vector. Could you also post the JSON conversions you are getting from the JSON module you used? This would help know what d1 and d2, dict1 and dict2 are. Commented Jan 12, 2023 at 21:25
  • @Bill Thanks for replying, Added JSON conversions above. please let me know if you need anything else Commented Jan 12, 2023 at 23:03
  • 1
    Are you trying to extract fields only if the subAttribDefId is present in the dict and matches in value with the other Dict? Do the Dicts need to be at the same nesting level in the JSON structure, or can they be at different nesting levels? Commented Jan 13, 2023 at 19:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.