function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hiteshwar marnihiteshwar marni 

how to parse below json response in apex

{ "destination_addresses" : [ "Bengaluru, Karnataka, India" ], "origin_addresses" : [ "Hyderabad, Telangana, India" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "576 km", "value" : 575574 }, "duration" : { "text" : "8 hours 33 mins", "value" : 30763 }, "status" : "OK" } ] } ], "status" : "OK" }


Thanks
 
rajat Maheshwari 6rajat Maheshwari 6

Hi Hiteshwar,

Use something like : - 

// Output in list Format

List<Object> lst_JsonParse = (List<Object>)Json.deserializeUntyped(Json string);

// use forloop 

for(Object obj : lst_JsonParse)
  {
     Map<String,Object> mp_StrObj = (Map<string,Object>)obj;

    system.debug(mp_StrObj.get('destination_addresses'));
}
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

 

 

Sandeep WaliaSandeep Walia
Hi Hiteshwar,

The wrapper class for your response would be:
public class AdressWrapper{
    public List<String>  destination_addresses;
    public List<String>  origin_addresses;
    public cls_rows[] rows;
    public String status;//OK
    class cls_rows {
        public cls_elements[] elements;
    }
    class cls_elements {
        public cls_distance distance;
        public cls_duration duration;
        public String status;   //OK
    }
    class cls_distance {
        public String text; //576 km
        public Integer value;   //575574
    }
    class cls_duration {
        public String text; //8 hours 33 mins
        public Integer value;   //30763
    }
}

You could use tools such as JSON to Apex (https://www.adminbooster.com/tool/json2apex) to convert a JSON response into apex wrapper in future.

Hope this helps,
Sandeep
K S R PAVAN KUMARK S R PAVAN KUMAR
Help required in parsing the Json
My json is
 
{
  "metadata": {
    "provider": "Oxford University Press"
  },
  "results": [
    {
      "id": "ace",
      "language": "en",
      "lexicalEntries": [
        {
          "entries": [
            {
              "etymologies": [
                "Middle English (denoting the ‘one’ on dice): via Old French from Latin as ‘unity, a unit’"
              ],
              "grammaticalFeatures": [
                {
                  "text": "Singular",
                  "type": "Number"
                }
              ],
              "homographNumber": "100",
              "senses": [
                {
                  "definitions": [
                    "a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
                  ],
                  "domains": [
                    "Cards"
                  ],
                  "examples": [
                    {
                      "registers": [
                        "figurative"
                      ],
                      "text": "life had started dealing him aces again"
                    },
                    {
                      "text": "the ace of diamonds"
                    }
                  ],
                  "id": "m_en_gbus0005680.006",
                  "short_definitions": [
                    "playing card with single spot on it, ranked as highest card in its suit in most card games"
                  ]
                },
                {
                  "definitions": [
                    "a person who excels at a particular sport or other activity"
                  ],
                  "domains": [
                    "Sport"
                  ],
                  "examples": [
                    {
                      "text": "a motorcycle ace"
                    }
                  ],
                  "id": "m_en_gbus0005680.010",
                  "registers": [
                    "informal"
                  ],
                  "short_definitions": [
                    "person who excels at particular sport or other activity"
                  ],
                  "subsenses": [
                    {
                      "definitions": [
                        "a pilot who has shot down many enemy aircraft"
                      ],
                      "domains": [
                        "Air Force"
                      ],
                      "examples": [
                        {
                          "text": "a Battle of Britain ace"
                        }
                      ],
                      "id": "m_en_gbus0005680.011",
                      "short_definitions": [
                        "pilot who has shot down many enemy aircraft"
                      ]
                    }
                  ],
                  "thesaurusLinks": [
                    {
                      "entry_id": "ace",
                      "sense_id": "t_en_gb0000173.001"
                    }
                  ]
                },
                {
                  "definitions": [
                    "(in tennis and similar games) a service that an opponent is unable to return and thus wins a point"
                  ],
                  "domains": [
                    "Tennis"
                  ],
                  "examples": [
                    {
                      "text": "Nadal banged down eight aces in the set"
                    }
                  ],
                  "id": "m_en_gbus0005680.013",
                  "short_definitions": [
                    "(in tennis and similar games) service that opponent is unable to return and thus wins point"
                  ],
                  "subsenses": [
                    {
                      "definitions": [
                        "a hole in one"
                      ],
                      "domains": [
                        "Golf"
                      ],
                      "examples": [
                        {
                          "text": "his hole in one at the 15th was Senior's second ace as a professional"
                        }
                      ],
                      "id": "m_en_gbus0005680.014",
                      "registers": [
                        "informal"
                      ],
                      "short_definitions": [
                        "hole in one"
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "etymologies": [
                "early 21st century: abbreviation of asexual, with alteration of spelling on the model of ace"
              ],
              "grammaticalFeatures": [
                {
                  "text": "Singular",
                  "type": "Number"
                }
              ],
              "homographNumber": "200",
              "senses": [
                {
                  "definitions": [
                    "a person who has no sexual feelings or desires"
                  ],
                  "domains": [
                    "Sex"
                  ],
                  "examples": [
                    {
                      "text": "both asexual, they have managed to connect with other aces offline"
                    }
                  ],
                  "id": "m_en_gbus1190638.004",
                  "short_definitions": [
                    "asexual person"
                  ]
                }
              ]
            }
          ],
          "language": "en",
          "lexicalCategory": "Noun",
          "pronunciations": [
            {
              "audioFile": "http://audio.oxforddictionaries.com/en/mp3/ace_1_gb_1_abbr.mp3",
              "dialects": [
                "British English"
              ],
              "phoneticNotation": "IPA",
              "phoneticSpelling": "eɪs"
            }
          ],
          "text": "ace"
        },
        {
          "entries": [
            {
              "grammaticalFeatures": [
                {
                  "text": "Positive",
                  "type": "Degree"
                }
              ],
              "homographNumber": "101",
              "senses": [
                {
                  "definitions": [
                    "very good"
                  ],
                  "examples": [
                    {
                      "text": "Ace! You've done it!"
                    },
                    {
                      "text": "an ace swimmer"
                    }
                  ],
                  "id": "m_en_gbus0005680.016",
                  "registers": [
                    "informal"
                  ],
                  "short_definitions": [
                    "very good"
                  ],
                  "thesaurusLinks": [
                    {
                      "entry_id": "ace",
                      "sense_id": "t_en_gb0000173.002"
                    }
                  ]
                }
              ]
            },
            {
              "grammaticalFeatures": [
                {
                  "text": "Positive",
                  "type": "Degree"
                }
              ],
              "homographNumber": "201",
              "senses": [
                {
                  "definitions": [
                    "(of a person) having no sexual feelings or desires; asexual"
                  ],
                  "domains": [
                    "Sex"
                  ],
                  "examples": [
                    {
                      "text": "I didn't realize that I was ace for a long time"
                    }
                  ],
                  "id": "m_en_gbus1190638.006",
                  "short_definitions": [
                    "asexual"
                  ]
                }
              ]
            }
          ],
          "language": "en",
          "lexicalCategory": "Adjective",
          "pronunciations": [
            {
              "audioFile": "http://audio.oxforddictionaries.com/en/mp3/ace_1_gb_1_abbr.mp3",
              "dialects": [
                "British English"
              ],
              "phoneticNotation": "IPA",
              "phoneticSpelling": "eɪs"
            }
          ],
          "text": "ace"
        },
        {
          "entries": [
            {
              "grammaticalFeatures": [
                {
                  "text": "Transitive",
                  "type": "Subcategorization"
                },
                {
                  "text": "Present",
                  "type": "Tense"
                }
              ],
              "homographNumber": "102",
              "senses": [
                {
                  "definitions": [
                    "(in tennis and similar games) serve an ace against (an opponent)"
                  ],
                  "domains": [
                    "Tennis"
                  ],
                  "examples": [
                    {
                      "text": "he can ace opponents with serves of no more than 62 mph"
                    }
                  ],
                  "id": "m_en_gbus0005680.020",
                  "registers": [
                    "informal"
                  ],
                  "short_definitions": [
                    "(in tennis and similar games) serve ace against"
                  ],
                  "subsenses": [
                    {
                      "definitions": [
                        "score an ace on (a hole) or with (a shot)"
                      ],
                      "domains": [
                        "Golf"
                      ],
                      "examples": [
                        {
                          "text": "there was a prize for the first player to ace the hole"
                        }
                      ],
                      "id": "m_en_gbus0005680.026",
                      "short_definitions": [
                        "score ace on hole or with"
                      ]
                    }
                  ]
                },
                {
                  "definitions": [
                    "achieve high marks in (a test or exam)"
                  ],
                  "examples": [
                    {
                      "text": "I aced my grammar test"
                    }
                  ],
                  "id": "m_en_gbus0005680.028",
                  "regions": [
                    "North American"
                  ],
                  "registers": [
                    "informal"
                  ],
                  "short_definitions": [
                    "achieve high marks in"
                  ],
                  "subsenses": [
                    {
                      "definitions": [
                        "outdo someone in a competitive situation"
                      ],
                      "examples": [
                        {
                          "text": "the magazine won an award, acing out its rivals"
                        }
                      ],
                      "id": "m_en_gbus0005680.029",
                      "notes": [
                        {
                          "text": "\"ace someone out\"",
                          "type": "wordFormNote"
                        }
                      ],
                      "short_definitions": [
                        "outdo someone in competitive situation"
                      ]
                    }
                  ]
                }
              ]
            }
          ],
          "language": "en",
          "lexicalCategory": "Verb",
          "pronunciations": [
            {
              "audioFile": "http://audio.oxforddictionaries.com/en/mp3/ace_1_gb_1_abbr.mp3",
              "dialects": [
                "British English"
              ],
              "phoneticNotation": "IPA",
              "phoneticSpelling": "eɪs"
            }
          ],
          "text": "ace"
        }
      ],
      "type": "headword",
      "word": "ace"
    }
  ]
}


I want to print the value of definitions. Please help in this regard.
Elad Kaplan 35Elad Kaplan 35
Hi Kumar,

In order to parse a JSON and get the value you need, you need to get to the relevant Node, holding the value.

What you can do, is to Deserialize the JSON into Maps of (String, Object) or List of Maps of (String, Object) , in case the Node is an array, where the key is top Node and the value is the Peice of JSON of this Node.

e.g 

"metadata": {
"provider": "Oxford University Press"
}

Map<String, Object> metadata_Map = (Map<String, Object>) JSON.deserializeUntyped('metadata');

This will give you a Map<String, Object> and you can fetch the value of 'provider' like this : 

String providerVal = (String) metadata_Map.get('provider');

Elad
sailokesh Thathurusailokesh Thathuru

Hi K S R PAVAN KUMAR,

I think it's a late response check the below link it might help you 

Parse JSON with Apex 
https://salesforce.stackexchange.com/questions/262947/parse-json-with-apex 
(https://github.com/open-force/jsonparse) 

Mohit MathuriyaMohit Mathuriya
I am getting the below response while hitting API endpoint. I wanted to store the value of token into a string.

"token": "eyJraWQiOiIyNlN1NHFMNnVVZmlkIiwibW9iaWxlX3VzZXIiXSwic3"

How can I achieve this?