MyNixOS website logo
Description

Cherry picking in JSON objects.

Simple single record picking out of nested JSON objects

jsop, JSON record cherry picker

JSOP is good for picking out a product type value from nested json objects

The jread memoize the keys path structure so jread f g should be curried to repeat on multiple values. The Value will be scanned only one time, despite the paths are always expressed from the root. Order is restored by a final lookup.

Example

Preamble


{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

import Data.Aeson
import Data.Aeson.Lens
import Data.String.Interpolate
import qualified Data.Text as T
import Generics.SOP
import Generics.SOP.TH
import JSOP.Parse
import Protolude hiding (All, optional, (:*:))
import Data.Maybe (fromJust)

Given we have a SOP encoding of the record (tuples are good).

data ABC = ABC Text Int Int deriving (Show, Eq)

deriveGeneric ''ABC

Then we need a product of pickers with the same shape as our product type.

In this case I choose to encode paths joining json keys with /

cherryPickABC :: NP (Parser Text) '[Text, Int, Int]
cherryPickABC =
  required "object 1 / a string" _String
    :* required "object 2 / a number" _Integral
    :* optional "object 4 / a number" 42 _Integral
    :* Nil

Given the next json structure

jsonWithABC :: Value
jsonWithABC = fromJust . decode $ [i| 
  {
    "object 1": 
      { "a string": "ciao"
      , "ignore me" : 34
      }
  , "object 2": 
      { "a number": 2
      , "object 3": {}
      }
  , "object 4": {
      "a plumber" :43
      } 
  }
  |]

We can cherry pick the scattered ABC with

abc :: ABC
Right abc = jread (T.splitOn " / ") cherryPickABC jsonWithAB

We can also rewrite an ABC value in the json

Writing optionals has an ad-hoc defined behaviour, in general writing is still to be thought in case of missing keys

jsonABC :: Value 
jsonABC = jwrite (T.splitOn " / ") cherryPickABC jsonWithABC (ABC "mamma" 44 103) 
Metadata

Version

0.2.0.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows