Arrays 在bash中将文本文件的数据转换为JSON数组
我希望在linux/bash平台上使用命令以以下JSON数组格式输出。有人能帮忙吗 文本文件中的数据Arrays 在bash中将文本文件的数据转换为JSON数组,arrays,json,bash,shell,Arrays,Json,Bash,Shell,我希望在linux/bash平台上使用命令以以下JSON数组格式输出。有人能帮忙吗 文本文件中的数据 test:test test1:test1 test4:test4 预期输出: {array : [ {test:test}, {test1:test1}, {test4:test4} ] } xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):subs
test:test
test1:test1
test4:test4
预期输出:
{array :
[
{test:test},
{test1:test1},
{test4:test4}
]
}
xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'
xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'
使用
jq
命令行JSON解析器:
<file jq -Rs '{array:split("\n")|map(split(":")|{(.[0]):.[1]}?)}'
{
"array": [
{
"test": "test"
},
{
"test1": "test1"
},
{
"test4": "test4"
}
]
}
使用jq
命令行JSON解析器:
<file jq -Rs '{array:split("\n")|map(split(":")|{(.[0]):.[1]}?)}'
{
"array": [
{
"test": "test"
},
{
"test1": "test1"
},
{
"test4": "test4"
}
]
}
假设您想要实际的JSON输出:
$ jq -nR '{array: (reduce inputs as $line ([]; . + [$line | split(":") | {(.[0]):.[1]}]))}' input.txt
{
"array": [
{
"test": "test"
},
{
"test1": "test1"
},
{
"test4": "test4"
}
]
}
假设您想要实际的JSON输出:
$ jq -nR '{array: (reduce inputs as $line ([]; . + [$line | split(":") | {(.[0]):.[1]}]))}' input.txt
{
"array": [
{
"test": "test"
},
{
"test1": "test1"
},
{
"test4": "test4"
}
]
}
创建JSON最好使用专用的JSON工具,该工具还可以理解原始文本。
是这样一种工具
XPath:
{array :
[
{test:test},
{test1:test1},
{test4:test4}
]
}
xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'
xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'
(x:lines($raw)
是tokenize($raw,\r\n?|\n')
的缩写,它创建了所有行的序列。)
XQuery:
{array :
[
{test:test},
{test1:test1},
{test4:test4}
]
}
xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'
xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'
另请参见Xidel online tester。创建JSON最好使用专用的JSON工具,该工具还可以理解原始文本。
是这样一种工具
XPath:
{array :
[
{test:test},
{test1:test1},
{test4:test4}
]
}
xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'
xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'
(x:lines($raw)
是tokenize($raw,\r\n?|\n')
的缩写,它创建了所有行的序列。)
XQuery:
{array :
[
{test:test},
{test1:test1},
{test4:test4}
]
}
xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'
xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'
另请参见Xidel online tester。此数据是如何生成的?请注意,“
不是”
。只需使用:
作为分隔符逐行读取输入,并以{“某物”:“某物”的形式输出即可
然后joni用逗号标出行。您的输出不是有效的JSON…我在问题中做了更改。请检查这是否更合理您的预期输出仍然是无效的JSON。如果您想要类似JSON的内容,但不是,请不要将其称为JSON。此数据是如何生成的?请注意,“
不是”
。只需以:
作为分隔符逐行读取输入,并以{“something”:“something”}
的形式输出它们,然后用逗号替换这些行。您的输出不是有效的JSON…我已对问题进行了更改。请检查这是否更有意义您的预期输出仍然无效。如果您想要类似JSON但不需要,请不要将其称为JSON。我可以将新的对(键/值)附加到现有数组“test5”:“test5”{“array”:[{“test”:“test”},{“test1”:“test1”},{“test4”:“test4”},{“test5”:test5“}]}我可以将新的对(键/值)追加到现有数组“test5”:“test5”{“array”:[{“test”:“test”},{“test1”:“test1”},{“test4”:“test4”},{“test5”:“test5”}]}