hjson-query-1.0.1: library for querying from JSON

Text.HJson.Query

Contents

Synopsis

Types

type Jsons = [Json]Source

Building

emptyObj :: JsonSource

create empty JSON object

(-->) :: String -> Json -> JsonSource

create JSON object

(<>) :: Json -> Json -> JsonSource

merge two JSON Objects

(<.>) :: Json -> Json -> JsonSource

recursive merge two JSON Objects

merges :: [Json] -> JsonSource

merge list JSON Objects

mergesRec :: [Json] -> JsonSource

recursive merge lists JSON Objects

Example:

 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 = mergsRec [j1, j2, j3, j4, j5, j6, j7]
 ex0 = pputJson merg

Result:

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

Filtering

isObj :: JFilterSource

filter JSON objects

isArr :: JFilterSource

filter JSON arrays

isStr :: JFilterSource

filter JSON strings

isStrBy :: (String -> Bool) -> JFilterSource

predicative filter JSON strings

isNum :: JFilterSource

filter JSON numbers

isNumBy :: Fractional a => (a -> Bool) -> JFilterSource

predicative filter JSON numbers

isBool :: JFilterSource

filter JSON Bool

isNull :: JFilterSource

filter JSON null

isPrimitive :: JFilterSource

filter primitive types

getFromKeys :: [String] -> JFilterSource

get elements from object with keys

Example:

 query1 = getFromKeys ["Europa", "America", "Africa"] 
 json1 = query1 merg 
 ex1 = pputJsons 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

getFromKey :: String -> JFilterSource

get elements from object with key

Filter Combinators

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

Example:

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

Result:

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

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

concat result two filters

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