hjson-query-0.4: library for querying from JSON

Text.HJson.Query

Synopsis

Documentation

type Jsons = [Json]Source

jEmpty :: JsonSource

create empty JSON object

jMerge :: Json -> Json -> JsonSource

merge two JSON Objects

jMergeRec :: Json -> Json -> JsonSource

recursive merge two JSON Objects

jMerges :: [Json] -> JsonSource

merge list JSON Objects

jMergesRec :: [Json] -> JsonSource

recursive merge lists JSON Objects

Example:

 import Text.HJson  hiding (toString)
 import Text.HJson.Query
 import Text.HJson.Pretty
 
 --  Parse JSON
 jParse :: String -> Json
 jParse jstr = case (fromString  jstr) of
     Left l  -> error l
     Right json -> json
 
 j0 = jParse "{}"
 j1 = jParse "{\"Europa\": {\"Ukraine\": [\"Kiyv\", \"Gitomir\", \"Lviv\"]}}"
 j2 = jParse "{\"Asia\": {\"Japan\": [\"Tokyo\"]}}"
 j3 = jParse "{\"Europa\": {\"UnitedKingdom\": [\"London\", \"Glasgow\"]}}"
 j4 = jParse "{\"Europa\": {\"Germany\": [\"Berlin\", \"Bonn\"]}}"
 j5 = jParse "{\"Africa\": {}}"
 j6 = jParse"{\"America\": {\"USA\": [], \"Canada\": [\"Toronto\"]}}"
 j7 = jParse "{\"Australia\": [\"Melburn\", \"Adelaida\"]}"
 merg = jMergesRec [j0, j1, j2, j3, j4, j5, j6, j7]
 ex0 = putStrLn $ toString "   " merg

Result:

{
   "Africa": {
   },
   "America": {
      "Canada": ["Toronto"],
      "USA": []
   },
   "Asia": {
      "Japan": ["Tokyo"]
   },
   "Australia": ["Melburn", "Adelaida"],
   "Europa": {
      "Germany": ["Berlin", "Bonn"],
      "Ukraine": ["Kiyv", "Gitomir", "Lviv"],
      "UnitedKingdom": ["London", "Glasgow"]
   }
}

isObj :: JFilterSource

filter JSON objects

isArr :: JFilterSource

filter JSON arrays Example

isStr :: JFilterSource

filter JSON strings

isNum :: JFilterSource

filter JSON numbers

isBool :: JFilterSource

filter JSON Bool

isNull :: JFilterSource

filter JSON null

isPrimitive :: JFilterSource

filter primitive types

getFromKey :: String -> JFilterSource

get elements from object with key

getFromKeys :: [String] -> JFilterSource

get elements from object with keys

Example:

 pprint js = mapM_ putStrLn $ map (toString "   ") js --pretty print 
 query1 = getFromKeys ["Europa", "America", "Africa"] 
 json1 = query1 merg 
 ex1 = pprint json1

Result:

 {
    "Germany": ["Berlin", "Bonn"],
    "Ukraine": ["Kiyv", "Gitomir", "Lviv"],
    "UnitedKingdom": ["London", "Glasgow"]
 }
 {
    "Canada": ["Toronto"],
    "USA": []
 }
 {
 
 }

getFromObj :: JFilterSource

get all elements from object

getFromArr :: JFilterSource

get all elements from array

getFromIndex :: Int -> JFilterSource

get element from array with index

getFromIndexes :: [Int] -> JFilterSource

get elements from array with indexes

getChildern :: JFilterSource

get all elements from object and array

(>>>) :: JFilter -> JFilter -> JFilterSource

filter combinators

Example:

 query2 = query1 >>> getFromObj 
 json2 = query2 merg
 ex2 = pprint json2

Result:

 ["Berlin", "Bonn"]
 ["Kiyv", "Gitomir", "Lviv"]
 ["London", "Glasgow"]
 ["Toronto"]
 []

(<+>) :: JFilter -> JFilter -> JFilterSource

filter combinators

orElse :: JFilter -> JFilter -> JFilterSource

filter combinators

when :: JFilter -> JFilter -> JFilterSource

filter combinators

guards :: JFilter -> JFilter -> JFilterSource

filter combinators

deepObj :: JFilter -> JFilterSource

tree traversal filter for object

deepArr :: JFilter -> JFilterSource

tree traversal filter for array

deep :: JFilter -> JFilterSource

tree traversal filter for objects and arrays

Example:

 -- Qwery:  All city Europa, America, Australia and Africa
 -- q31, q32, q33 is equal
 
 q31 = getFromKeys ["Europa", "America", "Africa", "Australia"] 
   >>> (getFromArr `orElse`  getFromObj)
   >>> (isStr `orElse` getFromArr)
 
 q32 = getFromKeys ["Europa", "America", "Africa", "Australia"] 
   >>> (getFromObj `when` isObj)
   >>> getFromArr
 
 q33 = getFromKeys ["Europa", "America", "Africa", "Australia"] 
   >>> 
 deep getFromArr

See also: www.haskell.org/haskellwiki/HXT |

debug :: JFilter -> String -> StringSource

pretty print query from JSON string

 debug (deep isPrimitive) "[1, [false, true, [null]]]" == "[1, false, true, null]"