Shell文本Json处理

命令工具jq

jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样,都可以让你轻松地把玩文本。它能轻松地把你拥有的数据转换成你期望的格式,而且需要写的程序通常也比你期望的更加简短。
jq是用C编写,没有运行时依赖,所以几乎可以运行在任何系统上。预编译的二进制文件可以直接在Linux、OS X和windows系统上运行,当然在linux和OS X系统你需要赋与其可执行权限;在linux系统中也可以直接用yum安装。

# linux 
yum install jq
# mac 
brew install jq

使用帮助

 $ jq -h
jq - commandline JSON processor [version 1.6]

Usage:	jq [options] <jq filter> [file...]
	jq [options] --args <jq filter> [strings...]
	jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

	$ echo '{"foo": 0}' | jq .
	{
		"foo": 0
	}

Some of the options include:
  -c               compact instead of pretty-printed output;
  -n               use `null` as the single input value;
  -e               set the exit status code based on the output;
  -s               read (slurp) all inputs into an array; apply filter to it;
  -r               output raw strings, not JSON texts;
  -R               read raw strings, not JSON texts;
  -C               colorize JSON;
  -M               monochrome (don't colorize JSON);
  -S               sort keys of objects on output;
  --tab            use tabs for indentation;
  --arg a v        set variable $a to value <v>;
  --argjson a v    set variable $a to JSON value <v>;
  --slurpfile a f  set variable $a to an array of JSON texts read from <f>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;
  --jsonargs       remaining arguments are JSON arguments, not files;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].

See the manpage for more options.

JSON转义

cat data.txt | sed 's/\\/\\\\/g' | sed 's/\"/\\\"/g' > data
{
 "product":"apple",
 "properties": "{\"type\":\"phone\"}"
}

转成字符串

'{\"product\":\"apple\", \"properties\": "{\\\"type\\\":\\\"phone\\\"}\"}'

CURL文本遍历发送请求

#!/bin/bash

#sed -i "s/\"/\\\"/g" data.txt

# 开始
while read line
 do
   message="["$line"]"
   echo $message
   curl -v -X POST -H 'content-type: application/json;charset=UTF-8' -H 'content-type: application/json' "https://mrds.robin.cn/mrds/repush/event/accept"  -d  "$message"
   sleep 0.001
  done < data.txt

你可能感兴趣的:(Shell,json,linux,服务器)