0

I have a file with following dataset.

{"table":{"string":"ABC"} "TYPE":{"string":"U"} "TS":{"string":"2019-08-19 12:45:12.006362"} "C_TS":{"string":"2019-08-19 07:45:13.631000"} "dpt":{"string":"12345"} "ID":{"string":"123456789"} "FUC":{"string":"ABC"} "QDSA":{"string":"0"} "SVCD":{"string":"S"} }

Expecting the output in below format. output expected :

table|type|TS|C_TS|dpt|ID|FUC|QDSA|SVCD

ABC|U|2019-08-19 12:45:12.006362|2019-08-19 07:45:13.631000|12345|123456789|ABC|0|S|

2
  • 1
    Delete this question, read How to Ask, look at some recent questions that received upvotes for examples if that's unclear, then try again.
    – Ed Morton
    Aug 19, 2019 at 6:08
  • 2
    If you input data is a JSON document, then please post that JSON document in the question.
    – Kusalananda
    Aug 19, 2019 at 8:05

3 Answers 3

0

Tried with below command and it worked fine

command

awk -F ":" '{print $3,$NF}' p.txt| sed 's/"\}*//g'| sed "s/,NAME/|/g"| sed -r "s/\s+//g"| sed '1i table|NAME'

output

table|NAME
ABC|A

ABC|A

ABC|B

ABC|A

ABC|B
7
  • Thanks for the reply.can you share command if i have more variables like table and NAME....there is no definite no. of key value pair.
    – Abhinandan
    Aug 19, 2019 at 8:14
  • @Abhinandan It's difficult to answer a question that is not showing all information. Please update your question.
    – Kusalananda
    Aug 19, 2019 at 8:22
  • i dont want to harcode table and Name in code....because there can be more key value pair in the data.
    – Abhinandan
    Aug 19, 2019 at 8:50
  • {"table":{"string":"ABC"},"NAME":{"string":"A"},"Dummy1":{"string":"ABC"},"Dummy2":{"string":"A"},"Dummy3":{"string":"ABC"},"Dummy4":{"string":"A"}}
    – Abhinandan
    Aug 19, 2019 at 8:50
  • @Abhinandan Please consider updating your questions. Clarifications added in comments are 1) not read, 2) poorly formatted, and 3) not part of the question. Also add the expected output given your new data.
    – Kusalananda
    Aug 19, 2019 at 9:11
0
$ { echo 'table|NAME'; jq -r '"\(.table.string)|\(.NAME.string)"' file; }
table|NAME
ABC|A
ABC|A
ABC|B
ABC|A
ABC|B

This uses jq to parse out the data from each JSON object, creating a string for each output line that contains the table name and NAME string found in the object.

The header is created using a simple echo.

1
  • Thanks for the help :)
    – Abhinandan
    Aug 20, 2019 at 2:57
0

Another jq answer:

{ echo 'table|NAME'; <file jq -r 'map(.string)|join("|")'; }

which takes advantage of map function for combining both table and NAME object, and join to concatenate both object string string.


Based on the last OP's question update, you would need this command (requires jq version >=1.5):

<file jq -r 'keys_unsorted,map(.string)|join("|")'
4
  • Thanks it helps :)
    – Abhinandan
    Aug 20, 2019 at 2:56
  • @Abhinandan Please look at the last update
    – oliv
    Aug 20, 2019 at 6:35
  • Can i do this with awk....jq installation is not allowed asthose are blocked by client
    – Abhinandan
    Aug 20, 2019 at 12:17
  • @Abhinandan awk is not the best tool to parse json data. Use a json aware tool or language (perl, python...) to perform this task.
    – oliv
    Aug 20, 2019 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .