I have a json file for some conversation with the user and an ai assistant,
{ "model": "meta-llama/llama-4-scout-17b-16e-instruct", "messages": [ { "role": "system", "content": "You are Julia, and you are a helpful ai. Here is your previous memory: " }, { "role": "user", "content": "hi" } ] } I wanted to edit the json directly from a batch file using some command line interface to add another text like the following in the messages array:
{ "role": "assistant", "content": "hi, how's your day going?" }I tried many different ways but I am encounter error on every attempt
What I have tried:
- Powershell:
$json = Get-Content -Raw -Path "body.json" | ConvertFrom-Json$newMessage = [PSCustomObject]@{ role = "assistant" content = "hello, how can i help you"}$json.messages +=$newMessage$json | ConvertTo-Json -Depth 10 | Set-Content -Path "body.json"Result:
{"model": "meta-llama/llama-4-scout-17b-16e-instruct","messages": [ {"role": "system","content": "You are Julia, and you are a helpful ai. Here is your previous memory: " }, {"role": "user","content": "hi" }, {"role": "assistant","content": "hello, how can i help you" } ]}The problem here is that it has to much gap between start and the code2. JQ command prompt interface
jq '.messages +=[{"role": "assistant", "content": "hi, how's your day going?"}]' body.jsonResult:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting end of file (Windows cmd shell quoting issues?) at <top-level>, line 1:'.messagesjq: 1 compile error- JJ Stream command prompt interface
type body.json | jj -v '{"role": "assistant", "content": "hi, how's your day going?"}' messages+Result:
jj - JSON Stream Editor 1.9.2unknown option argument: "assistant,"It can be a Powershell script inside a batch file but I don't want a Powershell script file to run outside the batch program.
NOTE: I am a beginner in this topic so please don't blame me for silly mistakes
NOTE: I know batch file is not the best method to edit json files but that what I am trying to accomplish