English 中文(简体)
具有 j格特质的地名
原标题:Fieldnames with special characters in jq string interpolation
  • 时间:2024-02-29 09:54:16
  •  标签:
  • json
  • jq

我有一份记录单,有这样的条目:

{"@timestamp":"2024-02-28T10:21:51.939Z", "log.level":"info", "msg": "done"}

我想把这支 j子变成这样的东西:

2024-02-28T10:21:51.939Z - info - done

This is the output I get from my shell commands:

$ msg= {"@timestamp":"2024-02-28T10:21:51.939Z", "log.level":"info", "msg": "done"} 

$ echo $msg | jq -r  "(.["log.level"])" 
jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1:
"(.["log.level"])"     
jq: 1 compile error

$ echo $msg | jq -r  "(.msg)" 
done

$ echo $msg | jq -r  "(.["@timestamp"])" 
jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1:
"(.["@timestamp"])"     
jq: 1 compile error

$ echo $msg | jq -r  "(.@timestamp)" 
jq: error: syntax error, unexpected QQSTRING_INTERP_END, expecting QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
"(.@timestamp)"              
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
"(.@timestamp)"   
jq: 2 compile errors

I m在乌班图使用巴什。 jq-1.6

问题回答

奥基,我确定了我自己的问题。 与手册不同的是,你不必逃脱。

$ echo $msg | jq -r  "(.["@timestamp"])" 
2024-02-28T10:21:51.939Z

$ echo $msg | jq -r  "(.["log.level"])" 
info

以下是一些途径:

# if you don t like working with blackslashes
echo $msg | jq -r  [.["@timestamp","log.level","msg"]]|join(" - ")          

# if you want all the values (which in your case you do)
echo $msg | jq -r  values|join(" - ") 

# to_entries if you want to manipulate value
echo $msg | jq -r  to_entries|map(.value)|join(" - ") 
echo $msg | jq -r  to_entries|map(.value|ascii_upcase)|join(" - ") 




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签