Julia: json ファイルの crud

#共通の関数#
text_manipulate.jl はこちら
[Julia: text ファイルの crud](https://qiita.com/ekzemplaro/items/5c7f674ee034a33e2553)

#Create#

```julia:json_create.jl
#! /usr/bin/julia
#
#	json_create.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
#
include("text_manipulate.jl")
#
# import Pkg;Pkg.add("JSON")
using JSON
# --------------------------------------------------------------------
function data_prepare_proc()
	dict_aa = Dict()
#
	dict_aa = dict_append_proc(dict_aa,"t0921","宇都宮",41295,"2013-8-12")
	dict_aa = dict_append_proc(dict_aa,"t0922","小山",38756,"2013-5-15")
	dict_aa = dict_append_proc(dict_aa,"t0923","佐野",71294,"2013-6-8")
	dict_aa = dict_append_proc(dict_aa,"t0924","足利",27138,"2013-7-21")
	dict_aa = dict_append_proc(dict_aa,"t0925","日光",74682,"2013-4-19")
	dict_aa = dict_append_proc(dict_aa,"t0926","下野",82951,"2013-10-14")
	dict_aa = dict_append_proc(dict_aa,"t0927","さくら",96823,"2013-5-24")
	dict_aa = dict_append_proc(dict_aa,"t0928","矢板",57926,"2013-2-12")
	dict_aa = dict_append_proc(dict_aa,"t0929","真岡",64187,"2013-11-14")
	dict_aa = dict_append_proc(dict_aa,"t0930","栃木",82354,"2013-7-04")
	dict_aa = dict_append_proc(dict_aa,"t0931","大田原",72681,"2013-9-17")
	dict_aa = dict_append_proc(dict_aa,"t0932","鹿沼",23749,"2013-7-20")
	dict_aa = dict_append_proc(dict_aa,"t0933","那須塩原",12759,"2013-3-12")
	dict_aa = dict_append_proc(dict_aa,"t0934","那須烏山",62531,"2013-8-17")
#
	
	return	dict_aa
end
# --------------------------------------------------------------------
println(stderr,"*** 開始 ***")
fname_out = ARGS[1]
println(fname_out)
#
dict_aa = data_prepare_proc()
json_str = JSON.json(dict_aa)
fp_out = open(fname_out,"w")
write(fp_out,json_str)
close(fp_out)
#dict_to_text_proc(dict_aa,fname_out)
#
println(stderr,"*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./json_create.jl /var/tmp/json/cities.json
```

#Read#

```julia:json_read.jl
#! /usr/bin/julia
#
#	json_read.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# import Pkg;Pkg.add("JSON")
using JSON
# --------------------------------------------------------------------
println(stderr,"*** 開始 ***")
fname_in = ARGS[1]
println(fname_in)
#
dict_aa = JSON.parsefile(fname_in)
dict_display_proc(dict_aa)
#
println(stderr,"*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./json_read.jl /var/tmp/json/cities.json
```

#Update#

```julia:json_update.jl
#! /usr/bin/julia
#
#	json_update.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# import Pkg;Pkg.add("JSON")
using JSON
# --------------------------------------------------------------------
println(stderr,"*** 開始 ***")
fname_json = ARGS[1]
key_in = ARGS[2]
population_in = ARGS[3]
println(fname_json)
println(key_in,'\t',population_in)
#
dict_aa = JSON.parsefile(fname_json)
#
if ( haskey(dict_aa,key_in))
	dict_aa = dict_update_proc(dict_aa,key_in,population_in)
	json_str = JSON.json(dict_aa)
	fp_out = open(fname_json,"w")
	write(fp_out,json_str)
	close(fp_out)
end
#
println(stderr,"*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./json_update.jl /var/tmp/json/cities.json t0931 45982100
```

#Delete#

```julia:json_delete.jl
#! /usr/bin/julia
#
#	json_delete.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# import Pkg;Pkg.add("JSON")
using JSON
# --------------------------------------------------------------------
println(stderr,"*** 開始 ***")
fname_json = ARGS[1]
key_in = ARGS[2]
println(fname_json)
println(key_in)
#
dict_aa = JSON.parsefile(fname_json)
#
if ( haskey(dict_aa,key_in))
	delete!(dict_aa, key_in)
	json_str = JSON.json(dict_aa)
	fp_out = open(fname_json,"w")
	write(fp_out,json_str)
	close(fp_out)
end
#
println(stderr,"*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./json_delete.jl /var/tmp/json/cities.json t0928
```