-1

I have to pass the below string to a python command

'[{
  "action": "DELETE",
  "is_enabled": true,
  "name": "qe_ta_rule_kYKco6_ObjectLifecyclePolicy",
  "object_name_filter": null,
  "time_amount": 1,
  "time_unit": "DAYS"
}]'

Is there a way to replace all the newline characters so that i becomes a 1 line String

Fredz
  • 25
  • 3

2 Answers2

1

This is not a valid python string:

'[{
  "action": "DELETE",
  "is_enabled": true,
  "name": "qe_ta_rule_kYKco6_ObjectLifecyclePolicy",
  "object_name_filter": null,
  "time_amount": 1,
  "time_unit": "DAYS"
}]'

Multi-line strings should be like:

'[{ \
  "action": "DELETE", \
  "is_enabled": true, \
  "name": "qe_ta_rule_kYKco6_ObjectLifecyclePolicy", \
  "object_name_filter": null, \
  "time_amount": 1, \
  "time_unit": "DAYS" \
}]'

If this is coming from the web, it probably is coming like: '[{ \n "action": "DELETE", \n ...'

Either way, you would replace the new lines like your_str.replace('\n', '')

GAEfan
  • 11,244
  • 2
  • 17
  • 33
0

You can use jq to achieve this:

Source JSON:

[
  {"empid": "1", "empname": "E1", "empsal": "100"},
  {"empid": "2", "empname": "E2", "empsal": "200"},
  {"empid": "3", "empname": "E3", "empsal": "300"}
]

Transformation:

cat source.json | jq -c '.[]' > target.json

Target JSON:

{"empid":"1","empname":"E1","empsal":"100"}
{"empid":"2","empname":"E2","empsal":"200"}
{"empid":"3","empname":"E3","empsal":"300"}
Soumendra Mishra
  • 3,483
  • 1
  • 12
  • 38