Skip to content

Instantly share code, notes, and snippets.

@pkoppstein
pkoppstein / js2json.rb
Created February 11, 2015 22:51
javascript-to-json
#!/usr/bin/env ruby
# Syntax: js2json.rb [PATHNAME]
#
# This ruby script can be used to evaluate a single javascript
# expression (possibly including comments, arithmetic operators, etc.)
# and print the result as a JSON string. This is useful for reading
# JSON-with-comments and javascript-style objects.
#
# The script will read from STDIN if no PATHNAME is specified.
@pkoppstein
pkoppstein / BigInt.jq
Last active January 9, 2023 04:50
BigInt library of functions for jq
# Copyright (c) 2014-2015 Peter Koppstein (pkoppstein at gmail dot com) 2015.05.02
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
@pkoppstein
pkoppstein / combine.jq
Last active August 5, 2021 02:13
a commutative and associative operator for combining two JSON objects
# This gist defines a jq filter for combining two JSON objects by resolving conflicts
# in a way that is appropriate for mapping relational tables to objects.
# "combine" as defined here is both commutative and associative.
# Example:
# { "id": 123, "son": "Son1", "surname": "S"}
# | combine({ "id": 123, "son": "Son2", "surname": "S"})
# produces: { "id": 123, "son": ["Son1",Son2], "surname": "S"}
# Combine two entities in an array-oriented fashion.
@pkoppstein
pkoppstein / combine.jq
Created August 6, 2014 22:14
A commutative and associative "addition" for JSON objects (written in jq)
# This gist defines a jq function that may be useful when migrating from a "relational" view of things to an "object" view.
#
# For example, suppose we have two JSON objects as follows, corresponding to a row in a relational table:
#
# { "id": 123, "surname": "S", "son": "Son1", }
# { "id": 123, "son": "Son2"}
#
# In this case, we want to merge the objects with the result:
#
# { "id": 123, "surname": "S", "son": ["Son1", "Son2"]}