Extracting information from Json

Post your questions and help other users.

Moderator: Martin

Post Reply
mikeuys
Posts: 14
Joined: 05 Sep 2016 22:17
Location: NYC

Extracting information from Json

Post by mikeuys » 30 Sep 2016 02:34

Okay so I'm a super newb and i know the fundamentals of JSON what it is about, etc, what i don't know is how to extract information out of it. Say this is my code:

Code: Select all

[
{
"app_name": "Google Play Music",
"buttons": [
"Thumbs up",
"Previous",
"Play",
"Next",
"Thumbs down"
],
"content_title": "I'm The Man Remix",
"content_title_big": "",
"id": 1,
"notification_category": "transport",
"notification_foreground_service": false,
"notification_is_group_summary": false,
"notification_large_icon": "android.graphics.Bitmap@1cf7273",
"notification_local_only": false,
"notification_ongoing": false,
"notification_sound_default": false,
"notification_sub_text": "I'm The Man",
"notification_text": "I'm The Man Remix\n50 Cent\nI'm The Man",
"notification_text_big": "I'm The Man Remix\n50 Cent\nI'm The ManThumbs up\nPrevious\nPlay\nNext\nThumbs down\n\n",
"notification_vibrate_default": false,
"notification_visibility": 1,
"notification_when": 1475202405420,
"package_name": "com.google.android.music",
"title": "",
"wearable_actions": [
"Previous",
"Play",
"Next"
]
},
{
"app_name": "Automagic Premium",
"buttons": [],
"content_title": "Automagic Premium",
"content_title_big": "",
"id": -3,
"notification_foreground_service": true,
"notification_is_group_summary": false,
"notification_local_only": false,
"notification_ongoing": true,
"notification_sound_default": false,
"notification_text": "Automagic Premium\nAutomagic service is running\n",
"notification_text_big": "Automagic Premium\nAutomagic service is running\n\n",
"notification_ticker_text": "Automagic Premium",
"notification_vibrate_default": false,
"notification_visibility": 0,
"notification_when": 0,
"package_name": "ch.gridvision.ppam.androidautomagic",
"title": "Automagic Premium",
"wearable_actions": []
}
]
So say this is my notification and i want to extract out:
"notification_ongoing": false,
How would i go about that, if I'm correct i need the EAP newest version? I already got that set up, i just want to take that false information and put it into a variable. I tried Googling with no luck.
"If i had to buy automagic all over again with my current knowledge i'd drop 10 dollars on the sexy app glllrrr"

User avatar
mbirth
Posts: 80
Joined: 17 Mar 2016 00:02
Location: Berlin, Germany
Contact:

Re: Extracting information from Json

Post by mbirth » 30 Sep 2016 08:44

You have to put your JSON code into a variable and then use a script like:

Code: Select all

data = fromJSON(variablewithjsonstring);
notification_ongoing = data[0]['notification_ongoing'];
As you can see from the data[0], this extracts the value from the first entry. To get the second one, use data[1]['notification_ongoing'] ... and so on.
BlackBerry KEY2, Android 8.1.0

rcfree
Posts: 43
Joined: 21 Mar 2013 16:51

Re: Extracting information from Json

Post by rcfree » 11 Jan 2017 11:33

I tested it here and it did not work this way.

Can someone help by demonstrating how to extract an information from a json file?

Thanks

User avatar
mbirth
Posts: 80
Joined: 17 Mar 2016 00:02
Location: Berlin, Germany
Contact:

Re: Extracting information from Json

Post by mbirth » 11 Jan 2017 11:59

rcfree wrote:Can someone help by demonstrating how to extract an information from a json file?
If you want to read text from a file, the Init Variable Text File is your friend. And after it's in a variable, use the fromJSON() Script function to parse it into an object.

If it doesn't work, check the error log and your JSON data for validity.
BlackBerry KEY2, Android 8.1.0

rcfree
Posts: 43
Joined: 21 Mar 2013 16:51

Re: Extracting information from Json

Post by rcfree » 11 Jan 2017 21:21

I had not copied the "[" and "]" from the beginning and the end of the content, so it made a mistake.
Now it worked.
Thank you!


Is there a way to manipulate a json file as in the link plugin below?

Average
Sum
Filtering
Sorting

Thank you again.

http://forum.joaoapps.com/index.php?res ... arted.168/

User avatar
mbirth
Posts: 80
Joined: 17 Mar 2016 00:02
Location: Berlin, Germany
Contact:

Re: Extracting information from Json

Post by mbirth » 11 Jan 2017 22:22

With for-loops and a few others, this should be a piece of cake. No need for AutoTools…

E.g. for the average age, it should be something like this (untested):

Code: Select all

sum = 0;
kids = myData["kindergarten"]["children"];
for (i in kids) {
    sum = sum + kids[i]["age"];
}
average = sum / length(kids);
BlackBerry KEY2, Android 8.1.0

Post Reply