skip to Main Content

I have next Json String which I want to decode

[{
    "material_id": 1193,
    "material_code": "AN00000211",
    "material_name": "MARGARITA PIZZA",
    "security_code": "192.168.1.6",
    "security_code_name": null,
    "bar_barcode": "100209",
    "price_value": 50.00000,
    "mat_auto_price": 0.00000,
    "group_code": "PIZZA",
    "cat_id": 2,
    "mat_name_lang1": "MARGARITA  PIZZA",
    "mat_name_lang2": "МАРГАРИТА ПИЦЦА",
    "mat_name_lang3": "MARGARITA  PIZZA",
    "mat_attributes": "",
    "spe_code2": "",
    "spe_code3": "",
    "spe_code4": ""
},
{
    "material_id": 1194,
    "material_code": "AN00000212",
    "material_name": "TOWUKLY PIZZA",
    "security_code": "192.168.1.6",
    "security_code_name": null,
    "bar_barcode": "100210",
    "price_value": 65.00000,
    "mat_auto_price": 0.00000,
    "group_code": "PIZZA",
    "cat_id": 2,
    "mat_name_lang1": "TOWUKLY PIZZA",
    "mat_name_lang2": "КУРИНАЯ ПИЦЦА",
    "mat_name_lang3": "TOWUKLY PIZZA",
    "mat_attributes": "[{"mat_attribute_id":"6904982A-EBA6-4A31-8D38-3B688ADB9D4E","mat_attribute_name":"Test2","mat_attribute_desc":"Test2","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"6D6FD94D-1498-4EB1-A939-E15CFCE4A0F8","mat_attribute_name":"Test1","mat_attribute_desc":"Test1","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"9CD224D1-A87B-40CE-AACE-6B551666C344","mat_attribute_name":"Test3","mat_attribute_desc":"Test3","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"}]",
    "spe_code2": "",
    "spe_code3": "",
    "spe_code4": ""
}]

When I using

List decoded = jsonDecode(result);

I got Format Exception: Unexpected character error on here

"mat_attributes": "[{"mat_attribute_id":"6904982A-EBA6-4A31-8D38-3B688ADB9D4E","mat_attribute_name":"Test2","mat_attribute_desc":"Test2","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"6D6FD94D-1498-4EB1-A939-E15CFCE4A0F8","mat_attribute_name":"Test1","mat_attribute_desc":"Test1","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"9CD224D1-A87B-40CE-AACE-6B551666C344","mat_attribute_name":"Test3","mat_attribute_desc":"Test3","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"}]",

How can I correctly decode like that Json String?

2

Answers


  1. Nested JSON in "mat_attributes" field, which is causing jsonDecode function to throw an exception. "mat_attributes" field contains string representation of a JSON array.

    To decode the JSON string correctly, you need to decode nested JSON in the "mat_attributes" field separately after decoding main JSON string.

    import 'dart:convert';
    
    void main() {
      String jsonString = '[{"material_id":1193,"material_code":"AN00000211","material_name":"MARGARITA PIZZA","security_code":"192.168.1.6","security_code_name":null,"bar_barcode":"100209","price_value":50.00000,"mat_auto_price":0.00000,"group_code":"PIZZA","cat_id":2,"mat_name_lang1":"MARGARITA PIZZA","mat_name_lang2":"МАРГАРИТА ПИЦЦА","mat_name_lang3":"MARGARITA PIZZA","mat_attributes":"","spe_code2":"","spe_code3":"","spe_code4":""},{"material_id":1194,"material_code":"AN00000212","material_name":"TOWUKLY PIZZA","security_code":"192.168.1.6","security_code_name":null,"bar_barcode":"100210","price_value":65.00000,"mat_auto_price":0.00000,"group_code":"PIZZA","cat_id":2,"mat_name_lang1":"TOWUKLY PIZZA","mat_name_lang2":"КУРИНАЯ ПИЦЦА","mat_name_lang3":"TOWUKLY PIZZA","mat_attributes":"[{"mat_attribute_id":"6904982A-EBA6-4A31-8D38-3B688ADB9D4E","mat_attribute_name":"Test2","mat_attribute_desc":"Test2","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"6D6FD94D-1498-4EB1-A939-E15CFCE4A0F8","mat_attribute_name":"Test1","mat_attribute_desc":"Test1","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"9CD224D1-A87B-40CE-AACE-6B551666C344","mat_attribute_name":"Test3","mat_attribute_desc":"Test3","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"}]","spe_code2":"","spe_code3":"","spe_code4":""}]';
    
      List<dynamic> decodedList = jsonDecode(jsonString);
    
      // Decode the nested JSON within the "mat_attributes" field
      decodedList.forEach((element) {
        if (element['mat_attributes'] != '') {
          element['mat_attributes'] = jsonDecode(element['mat_attributes']);
        }
      });
    
      print(decodedList);
    }
    

    In this code, first decode main JSON string into list of dynamic objects. Then, iterate over each element in list and decode nested JSON within "mat_attributes" field if it is not empty.

    Login or Signup to reply.
  2. Remove unexpected characters

    Use the replace all function on string.

    final fixedResult = result.replaceAll('"[', '[').replaceAll(']"', ']');
    
    List decoded = jsonDecode(fixedResult);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search