JSON 질의 라이브러리는 세 갈래로 구현된다. 1 작은 인터프리터(jsonata, GROQ 의 groq-js, mistql): 질의 문자열을 렉서가 토큰으로 자르고, 파서가 AST 로 만든다 — jsonata 는 Pratt 의 하향식 연산자 우선순위 파서를 쓴다. 평가기가 그 AST 를 걸으며 JSON 데이터에 적용한다. 예를 들어 Order[Price > 30].Product 는 경로 노드 아래 Order 단계(필터 Price > 30 이 붙음)와 Product 단계로 파싱되고, Order 배열의 원소마다 필터를 적용한 뒤 남은 원소의 Product 를 모은다. 2 엔진을 빌린다(defiant.js): JSON 을 XML 로 바꾼 뒤 XPath 질의를 브라우저 내장 XPath 엔진(node.evaluate)에 맡기고, 찾은 XML 노드를 다시 JSON 으로 바꾼다. 3 질의 언어가 아니라 색인이다(js-search): 문서의 글자를 소문자로 맞추고 단어로 자른 뒤 접두어로 펼쳐(cat 을 c, ca, cat 으로) 토큰에서 문서로 가는 역색인을 만든다. 검색할 때는 검색어를 같은 방식으로 자르고 토큰마다 문서 집합을 찾아 교집합을 낸 뒤 TF-IDF 로 정렬한다.


Footnotes

  1. XPath 표현식으로 JSON 검색 ↩

  2. JSON 쿼리·변환 언어. XPath 3.1의 위치 경로에서 영감 ↩

  3. 클라이언트 측 검색 ↩

  4. Sanity의 쿼리 언어. 여러 문서를 묶어 필요한 필드만 뽑는다 ↩

  5. Query JSON documents in the Terminal with GROQ | CSS-Tricks ↩

  6. JSON 유사 구조에서 계산하는 소형 내장 언어 ↩

#123

TIL — The power of JSON.stringify replacer parameter | pawelgrzybek.com

JSON.stringify 는 열거 가능한 자기 속성만 본다 — Set·Map 은 {} 가 되니 replacer 로 바꿔 넣는다. space 문자열은 10자에서 잘린다.1

JSON.stringify(dude, (key, value) =>
  value instanceof Set ? [...value] : value
);
JSON.stringify(dude, null, "🍆");

Footnotes

  1. JSON.stringify() - MDN: “Only enumerable own properties are visited. This means Map, Set, etc. will become “{}”. You can use the replacer parameter to serialize them to something more useful.“ / “Number values of space are clamped to 10, and string values are truncated to 10 characters.” ↩

#124

JSON Schema enables the confident and reliable use of the JSON data format.



Footnotes

  1. 스키마로 JSON 을 검증하는 라이브러리(Node·브라우저). draft-04 부터 2020-12 까지와 JSON Type Definition(RFC 8927)을 지원하고, 스키마를 검증 코드로 미리 만들어 두는 standalone 모드가 있다. ↩

  2. JSON Schema 를 주면 웹 폼을 선언적으로 만들어 주는 React 컴포넌트. Ant Design·MUI·Chakra·shadcn 등 UI 테마를 골라 쓰고, 검증은 @rjsf/validator-ajv8 — 위 ajv 를 쓴다. ↩

#126

SQLite JSON 태그 빈도 정렬 — json_each 가 배열을 원소당 한 행으로 펼친다. 다시 묶을 때 순서는 집계 함수 안의 ORDER BY 로만 보장된다1

-- 태그별 빈도 계산
SELECT json_each.value AS tag, COUNT(*) AS frequency
FROM example, json_each(example.tags)
GROUP BY json_each.value
ORDER BY frequency DESC, tag ASC;

-- 빈도순 정렬된 태그를 JSON 배열로 (집계 안 ORDER BY — SQLite 3.44+)
SELECT json_group_array(tag ORDER BY frequency DESC, tag ASC) AS tags_by_frequency
FROM (
  SELECT json_each.value AS tag, COUNT(*) AS frequency
  FROM example, json_each(example.tags)
  GROUP BY json_each.value
);

Footnotes

  1. Built-in Aggregate Functions - SQLite: “If no ORDER BY clause is specified, the inputs to the aggregate occur in an arbitrary order that might change from one invocation to the next.” · 3.44.0 릴리스 노트: “Aggregate functions can now include an ORDER BY clause after their last parameter.” ↩

#331
type JsonPrimitive = string | number | boolean | null
type JsonObject = { [Key in string]: JsonValue } & {
  [Key in string]?: JsonValue | undefined
}
type JsonArray = JsonValue[] | readonly JsonValue[]
type JsonValue = JsonPrimitive | JsonObject | JsonArray

예전에 JSON 타입 정의가 필요해서 찾아봤던 내용

  • JsonObject: 문자열 키와 JsonValue 타입의 값을 가진 JSON 객체를 정의
  • JsonArray: JsonValue 타입의 요소를 포함하는 JSON 배열을 정의
  • JsonPrimitive: 문자열, 숫자, 불린, 또는 null과 같은 유효한 JSON 기본 값을 정의
  • JsonValue: 유효한 JSON 값을 나타내며, JsonPrimitive, JsonObject, 또는 JsonArray로 구성

#376