2

I am creating a JSON parser in Haskell. I have followed this link https://hackage.haskell.org/package/aeson-0.8.0.0/docs/Data-Aeson.html and tried to parse the JSON string and got successful as there was a single object.But now i have to parse the data below But the problem is how to parse this large data as it contains objects and nested objects thus i cannot understand how to parse it, Please help me in this regard. Any sort of help will be highly appreciated!!

"apiVersion": "2.0",
"data": {
"updated": "2010-01-07T19:58:42.949Z",
"totalItems": 800,
"startIndex": 1,
"itemsPerPage": 1,
"items": [
{
"id": "hYB0mn5zh2c",
"uploaded": "2007-06-05T22:07:03.000Z",
"updated": "2010-01-07T13:26:50.000Z",
"uploader": "GoogleDeveloperDay",
"category": "News",
"title": "Google Developers Day US - Maps API Introduction",
"description": "Google Maps API Introduction ...",
"tags": [
"GDD07",
"GDD07US",
"Maps"
],
"thumbnail": {
"default": "http://i.ytimg.com/vi/hYB0mn5zh2c/default.jpg",
"hqDefault": "http://i.ytimg.com/vi/hYB0mn5zh2c/hqdefault.jpg"
},
"player": {
"default": "http://www.youtube.com/watch?vu003dhYB0mn5zh2c"
},
"content": {
"1": "rtsp://v5.cache3.c.youtube.com/CiILENy.../0/0/0/video.3gp",
"5": "http://www.youtube.com/v/hYB0mn5zh2c?f...",
"6": "rtsp://v1.cache1.c.youtube.com/CiILENy.../0/0/0/video.3gp"
},
"duration": 2840,
"aspectRatio": "widescreen",
"rating": 4.63,
"ratingCount": 68,
"viewCount": 220101,
"favoriteCount": 201,
"commentCount": 22,
"status": {
"value": "restricted",
"reason": "limitedSyndication"
},
"accessControl": {
"syndicate": true,
"commentVote": true,
"rate": true,
"list": true,
"comment": true,
"embed": true,
"videoRespond": "moderated"
}
}
]
}
2
  • 1
    to give a complete solution is quite some work - so maybe you can add what you tried so far or simplify this into a simple example with one nested object - I'm sure you'll get a good answer soon you can expand on your own Commented Nov 2, 2014 at 19:14
  • If you can just tell how to declare this array "data": { "updated": "2010-01-07T19:58:42.949Z", "totalItems": 800, "startIndex": 1, "itemsPerPage": 1, that will be helpfull!! Commented Nov 2, 2014 at 20:04

1 Answer 1

1

First of all, I want to note that your data isn't valid JSON.... It is missing enclosing curly brackets.... Add "{" at the beginning and "}" at the end to fix this.

Once you have that, using Data.Aeson to parse is very easy to do, just use decode. There is a hitch, though. decode has an overloaded output type, and you will have to specify which output you are interested in. To just get an answer, you can use Maybe Value.

decode dataByteString::Maybe Value

Note that the data needs to be in a ByteString (hence my naming).

The Data.Aeson package can be used to do much more, including parse to a specific non-generic type, but you haven't asked for that, so I won't get into it.

A small test program to parse at the command line would be:

import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as BLC

decodeToMaybeValue::BLC.ByteString->Maybe Value
decodeToMaybeValue = decode

main = do
  interact (show . decodeToMaybeValue . BLC.pack)

The only reason you need to redefine decode to decodeToMaybeValue is to specify its output type.

Sign up to request clarification or add additional context in comments.

5 Comments

i have added the relevant libraries but i have now got stuck in the parsing code as i tried this instance FromJSON Detail where parseJSON (Object v) = Detail <$> v .: "id" <*> v .: "type1" <*> v .: "name" <*> v .: "ppu" --{-# LANGUAGE XOverloadedStrings #-} and it worked but for this any clue for parsing an array??
This is a modification of the original question, you should probably spin that out as a new question, and make the use case as small as possible (ie- shrink the input data to something as small as possible that still has the problem).... I guarantee that a lot of people here will help if you do this.
what is the way to parse this data as i can compile it on the compiler directly but cannot compile it by loading it.
@k102041HamzaRais- I don't understand the question.... The code I gave you parses the data, the full version will compile, then you have to pipe the file into it (ie- in Linux/bash, type cat data.json | ./parseData, where the created executable is parseData.
"The Data.Aeson package can be used to do much more, including parse to a specific non-generic type, but you haven't asked for that, so I won't get into it" are there questions that address this clearly?

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.