我正在尝试转换一个看起来像这样的对象:
{
"123" : "abc",
"231" : "dbh",
"452" : "xyz"
}
Run Code Online (Sandbox Code Playgroud)
对于看起来像这样的csv:
"123","abc"
"231","dbh"
"452","xyz"
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用命令行工具jq,但似乎无法弄清楚如何进行分配.我设法得到了钥匙,jq '. | keys' test.json但无法弄清楚接下来该做什么.
问题是你无法将这样的ak:v对象直接转换为csv with @csv.它需要是一个数组,所以我们需要先转换为数组.如果键被命名,它会很简单,但它们是动态的,所以它不那么容易.
所以我试图在Elastic Beanstalk上部署dockerfile,但我无法通过这个错误 - "jq:error:无法迭代null".
Successfully built [myContainerId]
Successfully built aws_beanstalk/staging-app
[2015-01-29T10:35:59.494Z] INFO [16343] - [CMD-AppDeploy/AppDeployStage0/AppDeployPreHook/04run.sh] : Starting activity...
[2015-01-29T10:36:05.507Z] INFO [16343] - [CMD-AppDeploy/AppDeployStage0/AppDeployPreHook/04run.sh] : Activity execution failed, because: command failed with error code 1: /opt/elasticbeanstalk/hooks/appdeploy/pre/04run.sh
jq: error: Cannot iterate over null
Docker container quit unexpectedly after launch: Docker container quit unexpectedly on Thu Jan 29 10:36:05 UTC 2015:. Check snapshot logs for details. (Executor::NonZeroExitStatus)
at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor/exec.rb:81:in `sh'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor.rb:15:in `sh'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/executable.rb:63:in `execute!'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/hook-directory-executor.rb:29:in `block (2 levels) in run!' …Run Code Online (Sandbox Code Playgroud) 我有一个需要在某种条件下更新的json文件.
示例json
{
"Actions" : [
{
"value" : "1",
"properties" : {
"name" : "abc",
"age" : "2",
"other ": "test1"
}
},
{
"value" : "2",
"properties" : {
"name" : "def",
"age" : "3",
"other" : "test2"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我正在编写一个脚本,它使用Jq匹配值和更新,如下所示
cat sample.json | jq '.Actions[] | select (.properties.age == "3") .properties.other = "no-test"'
Run Code Online (Sandbox Code Playgroud)
输出(打印到终端)
{
"value": "1",
"properties": {
"name": "abc",
"age": "2",
"other ": "test1"
}
}
{
"value": "2",
"properties": {
"name": "def", …Run Code Online (Sandbox Code Playgroud) 我Cannot iterate over null (null)从下面的查询得到,因为.property_history在result对象中不存在.
.property_history在继续操作之前,如何检查密钥的存在map(...)?
我试过用类似的东西 sold_year= `echo "$content" | jq 'if has("property_history") then
map(select(.event_name == "Sold"))[0].date' else null end
原始查询:
sold_year=`echo "$content" | jq '.result.property_history | map(select(.event_name == "Sold"))[0].date'`
Run Code Online (Sandbox Code Playgroud)
JSON:
{
"result":{
"property_history":[
{
"date":"01/27/2016",
"price_changed":0,
"price":899750,
"event_name":"Listed",
"sqft":0
},
{
"date":"12/15/2015",
"price_changed":0,
"price":899750,
"event_name":"Listed",
"sqft":2357
},
{
"date":"08/30/2004",
"price_changed":0,
"price":739000,
"event_name":"Sold",
"sqft":2357
}
]
}
}
Run Code Online (Sandbox Code Playgroud) 这是我的config.json:
{
"env": "dev",
"dev": {
"projects" : {
"prj1": {
"dependencies": {},
"description": ""
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的bash命令:
PRJNAME='prj1'
echo $PRJNAME
jq --arg v "$PRJNAME" '.dev.projects."$v"' config.json
jq '.dev.projects.prj1' config.json
Run Code Online (Sandbox Code Playgroud)
输出:
prj1
null
{
"dependencies": {},
"description": ""
}
Run Code Online (Sandbox Code Playgroud)
所以$ PRJNAME是prj1,但第一次调用只输出null.
有人能帮我吗?
我喜欢使用jq过滤json文件:
jq . some.json
Run Code Online (Sandbox Code Playgroud)
给定json包含一个对象数组:
{
"theList": [
{
"id": 1,
"name": "Horst"
},
{
"id": 2,
"name": "Fritz"
},
{
"id": 3,
"name": "Walter"
},
{
"id": 4,
"name": "Gerhart"
},
{
"id": 5,
"name": "Harmut"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想过滤该列表只显示id为2和4的元素,因此预期输出为:
{
"id": 2,
"name": "Fritz"
},
{
"id": 4,
"name": "Gerhart"
}
Run Code Online (Sandbox Code Playgroud)
如何使用jq过滤json?我玩过选择和地图,但没有任何工作,例如:
$ jq '.theList[] | select(.id == 2) or select(.id == 4)' array.json
true
Run Code Online (Sandbox Code Playgroud) 我有一个功能,如:
getServers() {
curl -s -X GET ...
}
Run Code Online (Sandbox Code Playgroud)
输出是一个复杂的 JSON 字符串。我可以像这样解析 JSON:
serverId=$(getServers | jq -r ".[] | select(whatever...)")
echo $serverId
Run Code Online (Sandbox Code Playgroud)
但是如果我将函数调用的输出存储在一个变量中,我会收到一个错误:
servers=$(getServers)
echo $servers | jq .
# jq can not process this
# parse error: Invalid string: control characters from U+0000 through
echo "$servers" | jq .
# does not work either
Run Code Online (Sandbox Code Playgroud)
U+001F 必须在第 ...
即使我$servers包含与函数调用相同的值,也jq无法处理它。这里发生了什么事?
$ jq --version
jq-1.5
这不是一个重复的问题!这个问题的根源在于\r\n字符串中的存在。添加引号并不能解决问题。
将字符串通过管道输送到tr '\r\n' ' ':
servers=$(getServers)
echo $servers …Run Code Online (Sandbox Code Playgroud) 我想在timestamp这里使用jq JSON处理器摆脱这个领域.
[
{
"timestamp": 1448369447295,
"group": "employees",
"uid": "elgalu"
},
{
"timestamp": 1448369447296,
"group": "employees",
"uid": "mike"
},
{
"timestamp": 1448369786667,
"group": "services",
"uid": "pacts"
}
]
Run Code Online (Sandbox Code Playgroud)
白名单也适用于我,即 select uid, group
最终我真正喜欢的是具有如下唯一值的列表:
employees,elgalu
employees,mike
services,pacts
Run Code Online (Sandbox Code Playgroud) 我可以在[]内的json中获取第一个元素
$ echo '[{"a":"x", "b":true}, {"a":"XML", "b":false}]' | jq '.[1]'
{
"a": "XML",
"b": false
}
Run Code Online (Sandbox Code Playgroud)
但是如果json已经被反汇编(例如,在使用'select'过滤条目后),我如何选择单个条目并避免此处出现的错误?
$ echo '[{"a":"x", "b":true}, {"a":"x", "b":false},{"a":"XML", "b":false}]' | jq '.[] | select( .a == "x")'
{
"a": "x",
"b": true
}
{
"a": "x",
"b": false
}
$ echo '[{"a":"x", "b":true}, {"a":"x", "b":false},{"a":"XML", "b":false}]' | jq '.[] | select( .a == "x") | .[1]'
jq: error (at <stdin>:1): Cannot index object with number
Run Code Online (Sandbox Code Playgroud) 我有一个数组:
[
{
"AssetId": 14462955,
"Name": "Cultural Item",
"Description": "It\u0027s... you know... an item. People love items!!!",
"AbsoluteUrl": "http://www.roblox.com/Cultural-Item-item?id=14462955",
"PriceInRobux": "300",
"PriceInTickets": "",
"Updated": "3 years ago",
"Favorited": "370 times",
"Sales": "55",
"Remaining": "",
"Creator": "ROBLOX",
"CreatorAbsoluteUrl": "http://www.roblox.com/users/1/profile",
"PrivateSales": "3 Private Sellers",
"PriceView": 1,
"BestPrice": "666,666,666",
"ContentRatingTypeID": 0,
"IsServerSideThumbnailLookupInCatalogEnabled": true,
"IsLargeItem": false,
"IsThumbnailFinal": true,
"IsThumbnailUnapproved": false,
"ThumbnailUrl": "http://t3.rbxcdn.com/cfbab57cabc6090d078c5f7af403caaa",
"BcOverlayUrl": null,
"LimitedOverlayUrl": "http://images.rbxcdn.com/793dc1fd7562307165231ca2b960b19a.png",
"DeadlineOverlayUrl": null,
"LimitedAltText": "Limited",
"NewOverlayUrl": null,
"SaleOverlayUrl": null,
"IosOverlayUrl": null,
"XboxOverlayUrl": null,
"IsTransparentBackground": false,
"AssetTypeID": 8,
"CreatorID": 1, …Run Code Online (Sandbox Code Playgroud)