0

I am trying to extract google.sent_time from the following JSON , how do I get that value?
It seems like it's a raw payload, and not straightforward JSON, so wondering if there is a way to extract it?

{
  "notificationID": "677291f5-a784-43df-af2e-0067e9c",
  "title": "test 223",
  "body": "payload",
  "lockScreenVisibility": 1,
  "groupMessage": "",
  "fromProjectNumber": "819539062812",
  "priority": 5,
  "rawPayload": "{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1591849563191,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"i\\\":\\\"677291f5-a784-43df-af2e-0363a4067e9c\\\"}\",\"oth_chnl\":\"\",\"pri\":\"5\",\"vis\":\"1\",\"from\":\"819539062812\",\"alert\":\"payload\",\"title\":\"test 223\",\"grp_msg\":\"\",\"google.message_id\":\"0:1591849563205759%409ebbcaf9fd7ecd\",\"google.c.sender.id\":\"819539062812\",\"notificationId\":-1451117355}"
}
Bashir
  • 2,057
  • 5
  • 19
  • 44
Angela Heely
  • 419
  • 2
  • 4
  • 13
  • Looks like a JSON object in a JSON object - parse the outer object, grab the string, parse that as JSON too – Ryan M Jun 12 '20 at 03:10

4 Answers4

0

It is JSON, with some characters escaped so it can fit in to a string type field in your main JSON.

You can extract the string value of "rawPayload" field an feed that to whatever JSON parser you are using, or you can use a regex directly on the string to get just the value:

Here is a regex example:

Pattern p = Pattern.compile("google\\.sent_time.*:(\\d+)");
Matcher m = p.matcher(yourInputString);
m.find();
String sentTime = m.group(1);
Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • is something off with this regular expression? ddoesnt work for me, gives me an error on the regular expression being incorrect – Angela Heely Jun 14 '20 at 19:58
  • @AngelaHeely yes, it looks like I forgot a backslash \ and also forgot to call `find` before getting the group. I fixed the code and tested it with your example input, it should work now. – Lev M. Jun 14 '20 at 21:14
  • do you have any idea about this : https://stackoverflow.com/questions/62377933/how-do-i-construct-and-pass-raw-json-data-with-okhttpclient-in-android – Angela Heely Jun 15 '20 at 00:42
0

Get the rawPayload value as string from the original JSON and wrap it around JSONObject again:

public static void main(String[] args) throws Exception {
    String str = "...";     // JSON string here
    
    JSONObject json = new JSONObject(str);
    
    JSONObject payload = new JSONObject(json.getString("rawPayload"));
    System.out.println(payload.toString());
}

Output:

{
   "google.delivered_priority":"normal",
   "google.sent_time":1591849563191,
   "google.ttl":259200,
   "google.original_priority":"normal",
   "custom":"{\"i\":\"677291f5-a784-43df-af2e-0363a4067e9c\"}",
   "oth_chnl":"",
   "pri":"5",
   "vis":"1",
   "from":"819539062812",
   "alert":"payload",
   "title":"test 223",
   "grp_msg":"",
   "google.message_id":"0:1591849563205759%409ebbcaf9fd7ecd",
   "google.c.sender.id":"819539062812",
   "notificationId":-1451117355
}

The org.json library is used for the example.

Community
  • 1
  • 1
libanbn
  • 272
  • 2
  • 8
0

Use Gson json parser. What it does is map your json data to POJO class. But since you don't care about the other items or you don't wanna use Gson you'll have to have to go:

    JSONObject obj = new 
  JSONObject(YOUR_JSON_DATA);
   JSONObject notiObj = 
  obj.getJSONObject("rawPayload");
  String sentTime =  notiObj.getString("google.sent_time);

Cheers!

Jason
  • 444
  • 4
  • 6
  • this ddoesnt work. i get back a crash: .NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)' – Angela Heely Jun 14 '20 at 15:55
0

rawPayload looks like a valid JSON, you can check this using any online json formatter. You can try this :

val str = "{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1591849563191,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"i\\\":\\\"677291f5-a784-43df-af2e-0363a4067e9c\\\"}\",\"oth_chnl\":\"\",\"pri\":\"5\",\"vis\":\"1\",\"from\":\"819539062812\",\"alert\":\"payload\",\"title\":\"test 223\",\"grp_msg\":\"\",\"google.message_id\":\"0:1591849563205759%409ebbcaf9fd7ecd\",\"google.c.sender.id\":\"819539062812\",\"notificationId\":-1451117355}"

    try {
        val key = "google.sent_time"
        val json = JSONObject(str)
        Log.i(javaClass.simpleName, "key = ${json[key]}")
    } catch (e: Exception) {
        Log.e(javaClass.simpleName, "failed :", e)
    }
ghassenk
  • 129
  • 1
  • 3