eval()
is not necessary. This will work fine:
var date = new Date(parseInt(jsonDate.substr(6)));
The substr()
function takes out the /Date(
part, and the parseInt()
function gets the integer and ignores the )/
at the end. The resulting number is passed into the Date
constructor.
I have intentionally left out the radix (the 2nd argument to parseInt
); see my comment below.
Also, I completely agree with Rory's comment: ISO-8601 dates are preferred over this old format - so this format generally shouldn't be used for new development.
For ISO-8601 formatted JSON dates, just pass the string into the Date
constructor:
var date = new Date(jsonDate); //no ugly parsing needed; full timezone support
With Python 2.6+ you can do:
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
or, if the JSON is in a file, you can do:
python -m json.tool my_json.json
if the JSON is from an internet source such as an API, you can use
curl http://my_url/ | python -m json.tool
For convenience in all of these cases you can make an alias:
alias prettyjson='python -m json.tool'
For even more convenience with a bit more typing to get it ready:
prettyjson_s() {
echo "$1" | python -m json.tool
}
prettyjson_f() {
python -m json.tool "$1"
}
prettyjson_w() {
curl "$1" | python -m json.tool
}
for all the above cases. You can put this in .bashrc
and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'
.
Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys
flag to the end. I.e. ... | python -m json.tool --sort-keys
.
Best Answer
Update: With PostgreSQL 9.5, there are some
jsonb
manipulation functionality within PostgreSQL itself (but none forjson
; casts are required to manipulatejson
values).Merging 2 (or more) JSON objects (or concatenating arrays):
So, setting a simple key can be done using:
Where
<key>
should be string, and<value>
can be whatever typeto_jsonb()
accepts.For setting a value deep in a JSON hierarchy, the
jsonb_set()
function can be used:Full parameter list of
jsonb_set()
:path
can contain JSON array indexes too & negative integers that appear there count from the end of JSON arrays. However, a non-existing, but positive JSON array index will append the element to the end of the array:For inserting into JSON array (while preserving all of the original values), the
jsonb_insert()
function can be used (in 9.6+; this function only, in this section):Full parameter list of
jsonb_insert()
:Again, negative integers that appear in
path
count from the end of JSON arrays.So, f.ex. appending to an end of a JSON array can be done with:
However, this function is working slightly differently (than
jsonb_set()
) when thepath
intarget
is a JSON object's key. In that case, it will only add a new key-value pair for the JSON object when the key is not used. If it's used, it will raise an error:Deleting a key (or an index) from a JSON object (or, from an array) can be done with the
-
operator:Deleting, from deep in a JSON hierarchy can be done with the
#-
operator:For 9.4, you can use a modified version of the original answer (below), but instead of aggregating a JSON string, you can aggregate into a json object directly with
json_object_agg()
.Original answer: It is possible (without plpython or plv8) in pure SQL too (but needs 9.3+, will not work with 9.2)
SQLFiddle
Edit:
A version, which sets multiple keys & values:
Edit 2: as @ErwinBrandstetter noted these functions above works like a so-called
UPSERT
(updates a field if it exists, inserts if it does not exist). Here is a variant, which onlyUPDATE
:Edit 3: Here is recursive variant, which can set (
UPSERT
) a leaf value (and uses the first function from this answer), located at a key-path (where keys can only refer to inner objects, inner arrays not supported):Updated: Added function for replacing an existing json field's key by another given key. Can be in handy for updating data types in migrations or other scenarios like data structure amending.
Update: functions are compacted now.